Laravel |唯一验证where子句 [英] Laravel | Unique validation where clause

查看:101
本文介绍了Laravel |唯一验证where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅在company_id与随请求传递的company_id相同时,我试图验证存在的电子邮件地址的输入.

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.

我收到此错误...

SQLSTATE [42S22]:找不到列:1054'where子句'中的未知列'1'(SQL:从company_users中选择count(*)作为汇总,其中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

推荐答案

这里有一个粗略的想法,您可以实现自己的目标

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

您可以使用 Rule 类为您自定义验证规则.

You can use Rule class to customize the validation rule for you.

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

希望这会有所帮助

这篇关于Laravel |唯一验证where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆