Best practices to handle the Laravel request validation.

Laravel request
Add caption
In Laravel we have a class LaravelRequests to handle all the Http request validations which has a lot of functions which make is too easy to handle the validations in Laravel. Most of the developer define validation rules and validate the inputs in controller, which looks like:

It looks little messy and hard to manage if there are multiple fields coming as a input. So we can resolve this by moving all this mess into request file. How?
Open project directory in terminal and run

  • php artisan make:request StoreMyRequests
Now, move check app/Http/Requests there will be a file named as
StoreMyRequests.php. Now open this file and move validation code into this and it will looks like:-

Now, Use these validations in controller like:-
Now, All mess moved from controller and it increase the reuse ability of code. You can use same rules at multiple places.

Comments

Popular posts from this blog

Update Laravel version 5.5 to 5.8

Write SQL queries in Laravel migration.