P
P
paparoma2015-07-17 17:19:34
Laravel
paparoma, 2015-07-17 17:19:34

Filtering data in Laravel?

I have an action that pulls data from a database and displays it on a page. You need to add a form to this page to filter the results (some field should be from X to Y, etc.).
The problem is that I have no idea how to do it gracefully.
My form is sent using the GET method.
I check for existence of values, validate data with Validator:make(), if validation fails set default values, then build query with Query Builder. The result is a huge sheet of code in the controller.
How do you implement filtering in your projects?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dave, 2015-07-17
@djay

Everything related to building dynamic SQL queries should be strictly in the model. In the controller, the variables of the request itself should only be passed to this very model.
All this sheet, you just need to take out into the model.

public function filter()
{
     $validator = Validator::make(...);
    
     if ($validator->fails()) {
         // Ошибка
     } else {
         
         $data = Input::get();
         $result = Model::applyFilter($data);

         return View::make(..);
     }

}

V
Vyacheslav Plisko, 2015-07-17
@AmdY

Validation on the client, why should the user submit the form to find out that he has formatted something incorrectly. And on the server the data is already stupidly substituted and in case of hacks it will receive a message that nothing was found. There is no need to clutter up the server side with logic, which you then need to duplicate on the client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question