'email' => 'required|unique:users,email,'.$user->id
We all know that while we update an instance in database we should ignore its id when we are using unique validation rule, like the above example.
Now in my application I have an Agency model and an Employee model and each Agency has many Employee.
In my application I have a form to create and update Agencies and Employees.
Data sent by this form :
$request->validate([
'agency.name' => 'required|string',
'agency.phone' => 'required|string|unique:agencies,phone,'.$agency->id,
.
.
.
'employees.*.first_name' => 'required|string',
'employees.*.last_name' => 'required|string',
'employees.*.email' => 'required|unique:employees,email'
]);
Now, validating agency.phone is quite easy, because we only have 1 agency and we can easily pass $agency->id to ignore unique validation rule for this instance.
'agency.phone' => 'required|string|unique:agencies,phone,'.$agency->id
Now the problem that I'm facing is that I have multiple employees in my incoming data and I cannot use an id to ignore
'employees.*.email' => 'required|unique:employees,email' // cannot add $employee->id here in order to ignore while updating
I do have id of each employee in my incoming data, so how can I use it to ignore laravel validation rule for array incoming data like in this example?
HELP
The output of dd($request->all()) is :
array:2 [
"agency" => array:7 [
"id" => 5
"name" => "xxxxxxx"
"manager_name" => null
"manager_phone" => null
"phone" => null
"created_at" => "2020-10-23T00:53:13.000000Z"
"updated_at" => "2020-10-23T00:53:13.000000Z"
]
"employees" => array:1 [
0 => array:6 [
"id" => 1
"first_name" => "John"
"last_name" => "Doe"
"email" => "[email protected]"
"created_at" => "2020-10-23T00:53:13.000000Z"
"updated_at" => "2020-10-23T00:53:13.000000Z"
]
]
]
'employees.*.email' =>['required', Rule::unique( 'employees')->ignore( 'employees.*.id') ],'employees.*.email' => 'required|unique:employees,email,' . 'employees.*.id',