Laravel conditional unique validation

风流意气都作罢 提交于 2021-02-08 15:15:11

问题


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


回答1:


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

use Illuminate\Validation\Rule; 


来源:https://stackoverflow.com/questions/45810009/laravel-conditional-unique-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!