Support the ongoing development of Laravel.io →
Validation Laravel Requests
Last updated 2 years ago.
0

Hi Georg!

First of all, if you use the Mass Assignment for Storing/Updating Model's Data, every extra field passed in the storing/updating data will be automatically ignored.

So, in order to manage also the validation for your JSON Data, i would suggest you to use the FormRequest Class in order to validate the input as you want. As you can see in the example, you can use the dot notation to validate JSON nested data.

php artisan make:request StoreYourModelRequest

/**
 * Get the validation rules that apply to the request.
 *
 * @return array
 */
public function rules()
{
    return [
        'title' => 'required|unique:posts|max:255',
        'author.name' => 'required',
        'author.description' => 'required',
    ];
}

And so, in your controller, you can use it:

public function store(StoreYourModelRequest $request)
{
    YourModel::store($request->validated()); // Validated method return ONLY validated data, extra will be ignored.
}

Hope it helps!

Renny

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.