Warm tip: This article is reproduced from stackoverflow.com, please click
api laravel laravel-5.8 laravel-6.2 php

Best Practice for Laravel Validation

发布于 2020-04-22 10:30:14

I Create New form request and use this for all CRUD operations

and use $this->getMethod(); to check the different between requests

then I face logical issues

  1. Is all fields that required in store method should be required in update method ?

    => the question here is should the consumer of APIs should sent all keys object to update specific key

  2. If It shouldn't and the keys name sent from APIs is different from the Database table columns name

    => I can't use update($request()->all()); because the key isn't the same as columns name, then I need to loop on all request keys to ignore the key that have null - can be done also by multi-check -

So please what the best practice of that ??

Questioner
Sameh Mohamed Omr
Viewed
38
Prateik Darji 2020-02-06 21:27

Welcometo Stackoverflow.

There are many methods of validation in laravel,

What I prefer is not to write validation in controller and write it in separate request and use that request object as parameter.

Lets assume we are having user model and and name is required and unique in add and edit method of your controller you can simple use UserRequest object as describe below.

Create request class

php artisan make:request UserRequest

class UserRequeset extends Request
{
    /**
    * Get the validation rules that apply to the request.
    * @return array
    */
    public function rules()
    {
        $id = request('id') ?: 'NULL'; //To identify if request is for add or edit just take autoincremented id parameter form request.

         return [
             'name' =>'required|unique:users,name,'.$id
         ];


        // you can also customize your validation for different methods as below

        switch ($this->method()){
            case 'POST':
                return [
                    // validation for post method
                ];
            break;
            case 'PUT':
                return [
                    // validation for put method
                ];
            break;
            default:
                return [];
            break;
        }

    }

    public function messages()
    {
        return [
            "name.required" => "User name is required",
            "name.unique" => "User name should be unique"

            // or you can customize this using language

            "name.required" => __("user.required_message"),
            "name.unique" => __("user.unique_message")
        ];
    }
}

in UserController

use App\Http\Requests\UserRequest;

Class UserController extends Controller {

    public function add(UserRequest $request)
    {
        //Enter your code just after validation part no need of any validation code here
    }
    public function update(UserRequest $request)
    {
        //Enter your update code just after validation part no need of any validation code here
    }
}

Hope it's helpful.