How to validate array in Laravel? | Laravel Point

 

How to validate array in Laravel?

In Laravel, you can validate an array by using the validate method provided by the Validator class. Here's an example of how you could validate an array of input data in Laravel:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

public function store(Request $request)
{
    $validator = Validator::make($request->all(), [
        'items.*.name' => 'required|string',
        'items.*.quantity' => 'required|numeric',
    ]);

    if ($validator->fails()) {
        return redirect('your-route')
                    ->withErrors($validator)
                    ->withInput();
    }

    // Your code here...
}

 

In the validation rules, the items.*.name and items.*.quantity rules validate each element in the items array. The * symbol is used to specify that each element of the array should be validated.

Previous Post Next Post

Contact Form