← Back to list

Laravel Validation in a Web based App -Part 2

In Part One, we covered how to validate a simple JSON request. In this article, we’ll look at how to handle more complex requests, such as…

Shafi Asharaf · 2025-05-08 16:51 · 0 claps · 0.9 min read
#laravel #validation #nested-array #nested #json
Open on Medium ↗

Laravel Validation in a Web based App -Part 2

Image by juicy_fish on Freepik

Image by juicy_fish on Freepik

In Part One, we covered how to validate a simple JSON request. In this article, we’ll look at how to handle more complex requests, such as validating a JSON payload with a nested array of objects like the one below:

{
"order_items": [
        {
            "product_id": "8",
            "quantity": "1"
        },
        {
            "product_id":"6",
            "quantity": "2A"
        }
    ]
}

We’ll focus only on the validation part here. For the complete implementation, refer to Part One of this series.

use App\Models\Order;
use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
    'order_items.*.product_id' => 'required|exists:products,id',
    'order_items.*.quantity' => 'required|numeric',
]);

if ($validator->fails()) {
    return response()->json(['errors' => $validator->errors()], 422);
}

foreach ($request->order_items as $item) {
    OrderDetails::create([
        'product_id' => $item['product_id'],
        'quantity' => $item['quantity'],
    ]);
}

In Laravel, the * symbol is used to indicate that the validation rule should be applied to each item in a nested array. Each nested key must be specified in this format for Laravel to apply the rules correctly.

Refer to the Laravel Validation Documentation for more details on available validation rules and advanced usage.

Comment response for the given request.


메타데이터
post_id
e6e791ce0817
slug
laravel-validation-in-a-web-based-app-part-2-e6e791ce0817
url
https://medium.com/@shafia774/laravel-validation-in-a-web-based-app-part-2-e6e791ce0817
canonical_url
https://medium.com/@shafia774/laravel-validation-in-a-web-based-app-part-2-e6e791ce0817
author_url
https://medium.com/@shafia774
status
ok
fetched_at
2026-07-19 23:43:22