I need to validate with the rule unique all code of the list of children before update, I know how to do it for the parent:
$parent = Parent::find($id);
$childrens = $parent->childrens;
'code' => ['required', Rule::unique('parents', 'code')->ignore($parent->id)],
'childrens.*.code' => ['required', Rule::unique('childrens', 'code')],
There is any easy way to get the current IDs of children to ignore them like I did in the parent? Or any idea to allow update of the child?
CodePudding user response:
The solution for hasMany relation in update is:
for ($i = 0; $i < $childrens->count(); $i ) {
$rules['childrens.' . $i . '.code'] = ['required', Rule::unique('childrens', 'code')->ignore($childrens[$i]['id'])];
}