如何验证 Laravel 5.6 中两个字段的组合唯一性?

How to validate combined uniqueness of two fields in Laravel 5.6?

这是我的验证规则:-

$validationCondition    = array(
                            'fname'     => 'required|min:2',
                            'lname'     => 'required|min:2',
                            'email'     => 'required|email|unique:users,email',
                            'isd'       => 'required|unique:users,isd',
                            'mobile'    => 'required',
                            'password'  => 'required'
                        );
$validationMessages     = array(
                            'fname.required'    => 'Please provide first name',
                            'fname.min'         => 'First name should be atleast 3 characters long',

                            'lname.required'    => 'Please provide last name',
                            'lname.min'         => 'Last name should be atleast 3 characters long',

                            'email.required'    => 'Please provide email',
                            'email.email'       => 'Please provide a valid email id',
                            'email.unique'      => 'This email id has already been registered. Choose any other valid email id.',

                            'isd.required'      => 'The city name should be atleast 3 characters long',

                            'mobile.required'   => 'Please provide mobile number',

                            'password.required' => 'Please provide password'
                        );

在用户table中,isdmobile都要检查,确保联系电话是独一无二。

我该如何检查?我知道如何检查单个字段的唯一性,但不知道如何检查组合值的唯一性。

$validationCondition = array(
    ...
    'isd' => 'required',
    'mobile' => 'required|unique:users,mobile,NULL,id,isd,' . $request->isd,
    ...
);

mobileunique 规则的含义是:"value of mobile must be unique among all existing users that have isd of the same value that came in request"。

$request 这是您正在验证的传入请求的实例(显然)。

您可以像下面这样使用

'mobile'  => 'required|unique:users,mobile,NULL,id,mobile,isd'

获取更多详情from doc