'Laravel Validation unique/exists with different database connection

In the documentation, I saw you could set a connection for the unique rule which is great. However, the exists doesn't seem to follow the same logic. Take this for example:

$rules = [
    'username'         => 'required|max:40|unique:user',
    'name'             => 'sometimes|required',
    'email'            => 'required|email|max:255|unique:int.user',
    'password'         => 'sometimes|required|confirmed|min:6',
    'password_current' => 'sometimes|required'
];

The unique rule works GREAT in this instance. It uses my database connection called 'int' and calls the user table. HOWEVER, when the rules are reversed like so:

$rules['email'] = 'required|email|max:255|exists:int.user';

I got this error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'int.user' doesn't exist (SQL: select count(*) as aggregate from int.user where email = [email protected])

It's trying to call an int.user table instead of using the int database connection.

Is there a reason exists doesn't act the same way as unique? Thanks.



Solution 1:[1]

instead of using connection name you can try with straight Database name which is defined in "int" connection. faced similar problem and these way worked for me. like

$rules['email'] = 'required|email|max:255|exists:DB_Name.user';

Solution 2:[2]

You can use

'email'           => 'exists:mysql2.users|required'

Where mysql2 is second database settings array in the database.php file

Solution 3:[3]

Ultimately for Laravel 5.6.* you need to look at an existing instance of the model you are trying to validate, or specify ...

{db_connection_name}.{schema_name}.{table_name}

... to ensure that you are looking at the proper table.

Validation Example

validate it...

<?php

// for instance... 
//   maybe auth user is in a different db
//   = so you cannot validate with your default db connection
$default_user = Auth::user();

// pass the instance in order to allow Validator to qualify the proper connection/name
\App\Validation\User::validate($_POST, $default_user);

User Validation class

<?php
namespace App\Validation;

class User extends Validator
{
    /**
     * @param \Illuminate\Database\Eloquent\Model|string $mixed
     * @param string $default
     * @return string
     */
    public static function table($mixed,$default='default_connection.app_schema.users_table')
    {
        if($mixed instanceof \Illuminate\Database\Eloquent\Model){
            $table = $mixed->getConnectionName().'.'.$mixed->getTable();
        } else {
            if (! empty($mixed)) {
                $table = $mixed;
            } else {
                $table = $default;
            }
        }
        return $table;
    } 

    /**
     * validation to create a new user
     *
     * @param array $data
     * @param \App\User|string $mixed
     * @return array
     * @throws \Illuminate\Validation\ValidationException
     */
    public static function validate(array $data, $mixed='default_connection.app_schema.users_table'){
        return Validator::validate($data,[
            'username'         => 'required|max:40|unique:'.self::table($mixed),
            'name'             => 'sometimes|required',
            'email'            => 'required|email|max:255|unique:'.self::table($mixed),
            'password'         => 'sometimes|required|confirmed|min:6',
            'password_current' => 'sometimes|required'
        ]);
    }
}

Solution 4:[4]

Try it.

$rules = [
     'username'         => 'required|max:40|unique:connection_name.user',
     'name'             => 'sometimes|required',
     'email'            => 'required|email|max:255|unique:connection_name.user',
     'password'         => 'sometimes|required|confirmed|min:6',
     'password_current' => 'sometimes|required'
];

Solution 5:[5]

$default_connection = 'db_name';
    $rules = [
        'username'         => 'required|max:40|unique:{$default_connection}.user',
        'name'             => 'sometimes|required',
        'email'            => 'required|email|max:255|unique:int.user',
        'password'         => 'sometimes|required|confirmed|min:6',
        'password_current' => 'sometimes|required'
    ];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Omar Faruk
Solution 2 Daud khan
Solution 3 Artistan
Solution 4 Muhammad Kamran
Solution 5 user2980898