Multilingual E-commerce API using Laravel 8.0 – Part 8 Product Request Object

When an API request comes in we need to make sure that the data that is sent in the API requests is a valid data. In Laravel, we can handle data validation in the controller class.

But Laravel also provides a cleaner way of validating your input using Form request validation.

To generate a new form request validation use the following command

php artisan make:request Product

This will create ProductRequest.php class in app/Http/Requests folder of your app.

Let’s add some validations in the rules function.

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'product_name' => 'required',
            'product_price' => 'required',
            'product_description' => 'required'
        ];
    }

There are a tons of validation rules Laravel provides. Here’s a complete list of all the validation rules.

Series Navigation<< Multilingual E-commerce API using Laravel 8.0 – Passing Information into the headersMultilingual E-commerce API using Laravel 8.0 – Part 3 Product Controller >>