問題描述
我正在嘗試驗證存在的電子郵件地址的輸入,但僅當 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:選擇 count(*) 作為來自 company_users 的聚合,其中 email_address = myemail.com 和 1 <> company_id)>
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)
我在網上閱讀過,這樣做的方法是將驗證中的表和列關聯起來,這就是我正在做的.
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);
})],
希望能幫到你
這篇關于拉拉維爾 |唯一驗證 where 子句的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!