Laravel authentication is unique / exists with a different database connection

In the documentation, I saw that you can establish a connection for a unique rule that is excellent. However, the creature does not seem to adhere to 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'
];

In this case, the unique rule is GREAT. It uses my database connection "int" and calls the user table. HOWEVER, when the rules change like this:

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

I got this error:

SQLSTATE [42S02]: base table or view not found: 1146 The table 'int.user' does not exist (SQL: select count (*) as a collection from int.user, where email = haleybuggs6@gmail.com )

It tries to call the int.user table instead of using the int database connection.

, , ? .

+5
3

, "int". , .

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

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

mysql2 - database.php

0

, Laravel 5.6. * , , ...

{db_connection_name}.{schema_name}.{table_name}

... , .

...

<?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 Verification 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'
        ]);
    }
}
0
source

Source: https://habr.com/ru/post/1608594/


All Articles