Laravel Validating An Array in Update Method With Multiple Rows Needing Ignoring

You can easily create validation rules dynamically as you wish.

Here is example from my current project which requires unique field except self.

use Illuminate\Validation\Rule;

$unique_check = Rule::unique('brand_managers', 'email'); // table with some unique rows

//if you need ignore multiple rows/multiple conditions
$unique_check->where(function ($query) use ($brand) {
    return $query->whereNotIn('id', $brand->brand_managers->pluck('id')->toArray());
});

$request->validate([
    'emails.*' => [ // promocode unique string column
        'required',
        $unique_check
    ],
]);

Tags:

Php

Laravel