Laravel特定列的唯一关键字
I'm using "unique" keyword for validating unique users for employee_id in controllers, in my database there is column called company_id , while adding new user they will be set us some company_id ,when i add new user for my company employee id will be unique for my company itself , if employee_id is 4 for another company and i'm adding 4 for my company it must accept , it will check only for that particular company only.
$this->validate($request,
[
'name' => 'required',
'emp_id' => 'required|unique:users', (Here how can i check for particular company)
'email' => 'required|unique:users',
'role' => 'required',
]);
can anyone please help me ???
我正在使用“unique”关键字来验证控制器中employee_id的唯一用户,在我的数据库中有一个名为 company_id,在添加新用户时,他们将为我们设置一些company_id,当我为我的公司添加新用户时,员工ID对于我公司本身是唯一的,如果employee_id对于另一家公司是4而我为我的公司添加了4 接受,它只会检查该特定公司。 p>
$ this-> validate($ request,
[
'name'=>'required' ,
'emp_id'=>'必需|唯一:用户',(我在这里如何检查特定公司)
'电子邮件'=>'必需|唯一:用户',
'角色'=&gt ;'required',
]);
code> pre>
任何人都可以帮助我吗??? p>
div>
You should use the array syntax here and use a "custom" unique rule:
'emp_id' => [ "required", Rule::unique('users')->where(function ($query) use ($request) {
$query->where('emp_id', $request->emp_id)->where("company_id",$request->company_id);
}) ]
Something like this anyway
If emp_id
and company_id
is in request
'emp_id' => 'required|unique:users,emp_id|unique:users,company_id',
Check in docs : https://laravel.com/docs/master/validation#rule-unique
I assume emp_id
and company_id
are present in users
table and you are sending in request