@orpha
In Laravel, you can validate an array of objects using Laravel's FormRequest validation. Here's an example of how you can validate an array of objects:
1
|
php artisan make:request YourRequestName |
1 2 3 4 5 6 7 8 |
public function rules() { return [ 'products' => 'required|array', 'products.*.name' => 'required|string', 'products.*.price' => 'required|numeric', ]; } |
1 2 3 4 |
public function store(YourRequestName $request) { // Your logic } |
Now, when you make a request with an array of objects (e.g., using JSON in the request body), Laravel will automatically validate each object in the array according to the rules defined in the FormRequest class. If the validation fails, Laravel will return the validation errors in the response.