Search code examples
laravelvalidationunique

How to unique validation from multiple tables


I want to unique validation a input field from multiple table. But i don't know how to do this. I already try some way but nothing work. Here is my code.

$validate = Validator::make($request->all(), [
    'gender' => 'required|max:5',
    'email' => 'required|unique:customers,email|unique:subscribers,email|max:128',
]);

Solution

  • You can use the unique rule twice, like so:

    $validate = Validator::make($request->all(), [
        'gender' => 'required|max:5',
        'email' => 'required|unique:customers|unique:subscribers|max:128',
    ]);
    

    You also don't have to provide the column name, if you leave it unspecified laravel will assume the field name, which is also email in this case.