0

In a multi-tenant Laravel app, each tenant has its own database connection. So after the user has selected his database, I want to authenticate the user using Auth::loginUsingId. Still, no matter what I do, I cannot change the Users Model's connection to another default.

If I set the connection in the model, it does connect to the specific database, but I want this to be done dynamically.

Is there a way to specify the connection dynamically that Laravel's auth should use for the authentication?

6
  • Does this answer your question? Laravel Change Connection Dynamically Commented Jun 29, 2021 at 11:01
  • @PeterKrebs This solution works on other cases, like when interacting with the users model, but it doesn't work with laravel's Auth methods. Commented Jun 29, 2021 at 11:17
  • You need to provide more specific information. How does a user selects a database? What happens after that? Commented Jun 29, 2021 at 11:17
  • When logging in the user provides his username, password and database name. Commented Jun 29, 2021 at 11:18
  • If you change the default database connection with e.g. config([ 'database.default' => ... ]) you might need to run DB::reconnect after that to reset the connection Commented Jun 29, 2021 at 11:22

2 Answers 2

1

You could define another connection in your config/database.php file like this:

return array(
    'connections' => array(
        'mysql' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database1',
            'username'  => 'user1',
            'password'  => 'pass1'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),

        'second_db_connection' => array(
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'database2',
            'username'  => 'user2',
            'password'  => 'pass2'
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ),
    )

And change the User model to be like this:

class User extends Model {

    protected $connection = 'second_db_connection';
    
}
Sign up to request clarification or add additional context in comments.

4 Comments

I already have multiple connections for each tenant. The problem is that I want to specify the connection dynamically, since there are more than two connections.
Well, check this answer: stackoverflow.com/a/42199159/9453520
This solution works on other cases, like when interacting with the users model, but it doesn't work with laravel's Auth methods.
-1

Before authenticating the user, change the database connection temporarily for the current request only using Conifg::set

$db = "database_name";
Config::set("database.connections.mysql.database", $db);

5 Comments

I have already tried that and it doesn't work. It seems that it uses the default connection of the model and cannot be overwritten. I have also tried DB::setDefaultConnection($connection), but it also doesn't work...
does the other database have different username or password? if yes, then you will also have to update it using the same command. ("database.connections.mysql.username", "database.connections.mysql.password"). If not, then you will have to share the error with us from the log files.
The error is that it's trying to find the users table on the default connection database instead on the tenant's database.
Hi @HouZer did find away to get the auth to use the tenant db ??
Unfortunately this will not work. The problem is, that the Illuminate\Auth\EloquentUserProvider::retrieveById method will call the newModelQuery() method, what makes a query, but doesn't care about the model's connection parameter, so it will use the currently used database connection, and not that one, what is set by the model.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.