Search code examples
laravelformsvalidationlaravel-8facade

How to validate data (coming in request) from another (buyers) table using laravel 8


I have 2 different tables based on their types: User & Buyer. I am validating "buyers" data but it checks in the "users" table rather than in the "buyers" table, Is there any way to ask Validator Facade to check in the "buyers" table.

$validator = Validator::make($request->all(), [
                    'name' => 'required',
                    'user_type' => 'required',
                    'phone' => 'required|unique:users|max:11|min:11',
                    'email' => 'required|email|unique:users',
                    'password' => 'required|min:8',
                    'passowrd_confirmation' => 'required|same:password',
                ]);

Solution

  • https://laravel.com/docs/8.x/validation#specifying-a-custom-column-name

    According to the documentation, you can indicate a specific field of any of your tables for validation. In this way you can do:

    'name' => 'required:buyers, name'