阵列传入数据的Laravel唯一验证规则忽略

阵列传入数据的Laravel唯一验证规则忽略,laravel,Laravel,我们都知道,在更新数据库中的实例时,在使用唯一验证规则时,应该忽略其id,如上面的示例所示。 现在在我的应用程序中,我有一个代理模型和一个员工模型,每个代理都有许多员工。 在我的申请表中,我有一个创建和更新机构和员工的表格。 此表格发送的数据: 'email' => 'required|unique:users,email,'.$user->id 现在,验证agency.phone非常简单,因为我们只有一个agency,我们可以轻松地传递$agency->id忽略此实例的唯一验证规

我们都知道,在更新数据库中的实例时,在使用唯一验证规则时,应该忽略其id,如上面的示例所示。
现在在我的应用程序中,我有一个代理模型和一个员工模型,每个代理都有许多员工。
在我的申请表中,我有一个创建和更新机构和员工的表格。
此表格发送的数据:

'email' => 'required|unique:users,email,'.$user->id
现在,验证
agency.phone
非常简单,因为我们只有一个agency,我们可以轻松地传递
$agency->id
忽略此实例的唯一验证规则

$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'
]);
现在我面临的问题是,我的传入数据中有多个员工,我不能使用id来忽略

'agency.phone' => 'required|string|unique:agencies,phone,'.$agency->id
我的传入数据中确实有每个员工的id,所以如何使用它来忽略像本例中这样的数组传入数据的laravel验证规则

帮助
dd($request->all())
的输出是:

'employees.*.email' => 'required|unique:employees,email' // cannot add $employee->id here in order to ignore while updating
只需验证两次!:)

您可以首先验证所有不需要知道员工ID的内容。像这样:


您可以使用自定义验证规则,您是否将所有员工id作为一个数组?我更新了问题,您可以从我的表单@staTry this
'employees.*.email'=>['required',rule::unique('employees')->ignore('employees.*.id'),
这也可以工作
'employees.*.email'=>'required | unique:employees,email'.'employees.*.id',
@sta您永远不应该将用户数据传递给ignore,这将使其容易受到SQL注入攻击。
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" => "doctor@doctor.com"
      "created_at" => "2020-10-23T00:53:13.000000Z"
      "updated_at" => "2020-10-23T00:53:13.000000Z"
    ]
  ]
]