Home > Back-end >  Laravel Validation unique query
Laravel Validation unique query

Time:03-22

I want to customize the laravel validation unique query

like User::where('type','admin')

Here is My Code

$validator = Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required|email|unique:users', //need to customize
        'password' => 'required',
        'confirm_password' => 'required|same:password',
    ]);

    if ($validator->fails()) {
        $res = [
            'bool' => false,
            'message' => $validator->errors()->first(),
        ];
        return response()->json($res);
    }

CodePudding user response:

You can use this

https://laravel.com/docs/8.x/validation#rule-exists

use Illuminate\Validation\Rule;

    $validator = Validator::make($request->all(), [
            'name' => 'required',
             email' => ['required','email',  Rule::unique('users')->where('type','admin')],
            'password' => 'required',
            'confirm_password' => 'required|same:password',
        ]);

        if ($validator->fails()) {
            $res = [
                'bool' => false,
                'message' => $validator->errors()->first(),
            ];
            return response()->json($res);
        }
  • Related