简体   繁体   中英

Laravel 5.5 unique validation rule on seperate table with different column name

So I have users and companies. A user belongs to one company.

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.

Here is my register function:

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);
        }

}

The field I want to compare against is the companies.name to check for uniqueness.

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',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM