# Laravel validator implements joint index uniqueness of two or more fields

Time：2019-12-1

The validation of laravel is still very easy to use. Using validator can easily validate forms. It provides unique verification, but only one field can be verified by default. How can we achieve the uniqueness of complex conditions when encountering the joint index of two or more fields

The implementation of validator’s complex uniqueness

We can use custom rules to customize validation rules, such as this:


[...]
$where = [ 'name' =>$request->name,
'phone' => $request->phone ];$this->validate($request, [ "phone" => [ "required", Rule::unique('table_name') ->where(function ($query) use ($where) { return$query->where(\$where);
})
],
]);
[...]

First of all, we change the original string form to array form, and use rule to customize new rules in the array. It is obvious that the unique () method comes from the definition of unique, and then the parameter is the name of the table, followed by a where function, which uses closures. Anonymous function queries whether the results satisfying both conditions exist, and returns the query results.

In this way, we complete the custom complex uniqueness verification.

summary

The above is the Laraway validator introduced by Xiaobian to you to realize the unique joint index of two or more fields, hoping to help you. If you have any questions, please leave a message to me, and Xiaobian will reply to you in time. Thank you very much for your support of the developepaer website!