I am trying to add a unique rule on an array of data and wants to ignore the unique rule to a given id when updating the same record.
When creating a new record I am using rules like
$rules = [
'provider.*.link' => 'required|url|unique:providers,link',
];
and my data array looks like
[provider] => [
[0] =>[
[link] => http://mysite.local/1
[1] =>[
[link] => http://mysite.local/1
]
which works fine. But, when updating same data I can't figure it out how to ignore unique rule to their respective ids.
I know how to do it when there's no array like
'link' => 'required|url|unique:providers,link,' . $id,
but not sure how to use it when data is an array.
My update data array looks like
[provider] =>
[
[0] =>
[
[id] => 3
[link] => http://mysite.local/1
]
[1] =>
[
[id] => 4
[link] => http://mysite.local/1
]
]
I am using Lumen and using the same function for creating and updating records. Is there's a way to achieve this?
You could do this:
$rules = [
....
];
$providers = request('provider');
for ($i = 0; $i < count($providers); $i++) {
$rules['provider.' . $i . '.link'] = 'required|url|unique:providers,link,' . $providers[$i]['id'];
}
return $rules;