laravel 拉拉维尔 | 唯一验证 where 子句

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49211988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:29:56  来源:igfitidea点击:

Laravel | Unique validation where clause

mysqllaravelapilumen

提问by Adnan Mumtaz

I am trying to validate the input of a email address that exists but only when the company_id is the same as the company_id which is passed in with the request.

我正在尝试验证存在的电子邮件地址的输入,但仅当 company_id 与随请求传入的 company_id 相同时。

I am getting this error...

我收到此错误...

SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'where clause' (SQL: select count(*) as aggregate from company_users where email_address = myemail.com and 1 <> company_id)

SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 '1'(SQL:选择 count(*) 作为来自 company_users 的聚合,其中 email_address = myemail.com 和 1 <> company_id)

I have read online and the way to do it is to associate the table and the column inside of the validation which is what I am doing.

我在网上阅读过,这样做的方法是将验证中的表和列相关联,这就是我正在做的。

This is my current code...

这是我当前的代码...

required|email|unique:company_users,email_address,company_id,' . $request->company_id

回答by Adnan Mumtaz

Here is a rough idea with this you can achieve what you

这是一个粗略的想法,你可以实现你的目标

You can use Ruleclass to customize the validation rule for you.

您可以使用Ruleclass 为您自定义验证规则。

'email' => ['required', 'string', 'email', 'max:191',Rule::unique('users')->where(function ($query) use ($request) {
    return $query->where('company_id', $request->company_id);
})],

Hope this helps

希望这可以帮助

回答by Sletheren

It has to be like:

它必须是这样的:

required|email|unique:company_users,email_address,' . $request->company_id

The format is:

格式为:

unique:table,column,'id to exclude'

Or if you want to validate based on a column you can do a separate query:

或者,如果您想基于列进行验证,您可以执行单独的查询:

$exists = Model::where('company_id','!=',$request->company_id)->where('email',$request->email)->count()
if($exists) {
  // return an error or a validation error
}

回答by Wreigh

This should answer your question.

这应该回答你的问题。

'required|email|unique:company_users,email_address,NULL,id,company_id,' . $request->company_id

This means, the email must be uniquewhile ignoring a row with an idof NULLand where its company_idis the same as $request->company_id.

这意味着,电子邮件必须是唯一的,同时忽略带有idofNULLcompany_id与 相同的行$request->company_id

The exceptparameter ignores rows to compare against the validation. E.g., an email address of [email protected]has already been taken by user 1but you did this:

不同的参数忽略行来比较验证。例如,[email protected]用户已经使用了 的电子邮件地址,1但您这样做了:

'unique:company_users,email_address,1'

It will ignore user with idof 1. So inputting the same email will still pass since the existinguser with the same email has been ignored.

它将忽略 1 的用户id。因此输入相同的电子邮件仍然会通过,因为具有相同电子邮件的现有用户已被忽略。

Which is not the same in your case, because you just dont want to ignore a special row, but you want to ignore a row based on a condition. So that is how this answer helps you, by adding more field validations afterthe ignoreparameter.

这与您的情况不同,因为您只是不想忽略特殊行,但您想根据条件忽略行。这就是这个答案如何帮助你,通过在ignore参数之后添加更多的字段验证。

You might want to take a look at this: laravel 4: validation unique (database) multiple where clauses

你可能想看看这个:laravel 4: validation unique (database) multiple where clauses

回答by Marwelln

Make $request->company_ida string by adding 'single quotes around your variable. Otherwise MySQL will think is a column in your table.

$request->company_id通过添加一个字符串,'在你变单引号。否则 MySQL 会认为是表中的一列。

required|email|unique:company_users,email_address,company_id,"'" . (int) $request->company_id . "'"