Laravel条件唯一验证 [英] Laravel conditional unique validation
本文介绍了Laravel条件唯一验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Laravel 5.4,我需要验证仅当role_id字段不是999时,users表上的email字段是否唯一.
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:
找不到类'App \ Http \ Controllers \ Auth \ Rule'
Class 'App\Http\Controllers\Auth\Rule' not found
我在做什么错了?
谢谢
推荐答案
您必须在名称空间后文件顶部的 use 中包含 Rule
类声明:
You have to include the Rule
class with a use at the top of your file after the namespace declaration:
use Illuminate\Validation\Rule;
这篇关于Laravel条件唯一验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文