N
N
netrox2018-09-19 12:17:16
Laravel
netrox, 2018-09-19 12:17:16

How to check if parameters were passed in Laravel From Request?

I am using Laravel. In the controller for filtering data, I check whether the request is empty, if it is empty, then I redirect, and if not, then I check the validity. Now I'm going to move this to a separate Form Request . How to check if the incoming request is empty using From Request. Similar code in the controller:

if(empty($request->all()))
        {
            return redirect('/');
        }

        $this->validate($request,
            [
                'department' => 'integer|nullable',
                'date' => 'date|nullable'
            ]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kuznetsov, 2018-09-19
@dima9595

Laravel already has a built-in and normal form validation system. Specify the required field in the validation rules and that's it. If the field is empty, it will redirect back with an error.
How I do:
Create a new file via php artisan make:request NameRequest. In it I create rules of validation of the form. In the controller (or somewhere else), I add the NameRequest to the method and it works itself out. Those. no need to clutter up the controller with form validations.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question