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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community