Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel conditional unique validation

Working with Laravel 5.4, I need to validate if the email field on the users table is unique only if the role_id field is not 999.

In the Register Controller I did this:

      return Validator::make($data, [
            'first_name' => 'required|max:255',
            'last_name' => 'required|max:255',
            'email' => [
                'required',
                'email',
                'max:255',
                Rule::unique('users')->where(function($query) {
                  $query->where('role_id', '<>', 999);
    })
   ],

            'phone' => 'required|phone',
            'password' => 'required|min:6|confirmed'
        ]);

But when I try to register I get this error:

Class 'App\Http\Controllers\Auth\Rule' not found

What am I doing wrong?

Thank you

like image 621
Sigal Zahavi Avatar asked Aug 22 '17 06:08

Sigal Zahavi


1 Answers

You have to include the Rule class with a use at the top of your file after the namespace declaration:

use Illuminate\Validation\Rule; 
like image 85
thefallen Avatar answered Sep 22 '22 13:09

thefallen