問題描述
所以我有用戶和公司.一個用戶屬于一個公司.
So I have users and companies. A user belongs to one company.
我想驗證用戶注冊,以便他們用來注冊的 business_name
字段在 companys
表中是唯一的,目標是不允許用戶創建重復公司.
I want to validate a user registration so that the business_name
field they use to register is unique in the companies
table, the goal is to not allow users from creating duplicate companies.
這是我的注冊函數:
public function register(Request $request)
{
$validator = Validator::make($request->all(), [
'first_name' => 'required',
'last_name' => 'required',
'business_name' => 'required|unique:companies',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6',
]);
if ($validator->fails()) {
return response()->json(['error'=>$validator->messages()], 401);
}
}
我要比較的字段是用于檢查唯一性的 companies.name
.
The field I want to compare against is the companies.name
to check for uniqueness.
這可能嗎?目前它正在嘗試在 companys
表中查找 business_name
.
Is this possible? At the moment it is trying to look for business_name
in the companies
table.
推薦答案
沒關系,設法弄明白了.只需要一個額外的參數來指定列名:
Never mind, managed to figure it out. Just needed an extra parameter to specify the column name:
'business_name' => 'required|unique:companies,name',
這篇關于Laravel 5.5 對具有不同列名的單獨表的唯一驗證規則的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!