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?

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
HouZer
  • 1
  • 1
  • Does this answer your question? [Laravel Change Connection Dynamically](https://stackoverflow.com/questions/42198046/laravel-change-connection-dynamically) – Peter Krebs Jun 29 '21 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. – HouZer Jun 29 '21 at 11:17
  • You need to provide more specific information. How does a user selects a database? What happens after that? – apokryfos Jun 29 '21 at 11:17
  • When logging in the user provides his username, password and database name. – HouZer Jun 29 '21 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 – apokryfos Jun 29 '21 at 11:22
  • This generally works, but it doesn't work when using Laravel's Auth to login. – HouZer Jun 29 '21 at 11:28

2 Answers2

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';
    
}
  • 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. – HouZer Jun 29 '21 at 10:52
  • Well, check this answer: https://stackoverflow.com/a/42199159/9453520 –  Jun 29 '21 at 11:04
  • This solution works on other cases, like when interacting with the users model, but it doesn't work with laravel's Auth methods. – HouZer Jun 29 '21 at 11:13
  • See this thread: https://laracasts.com/discuss/channels/laravel/using-auth-on-a-different-connection-or-database –  Jun 29 '21 at 12:28
0

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);
Amir
  • 721
  • 6
  • 21
  • 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... – HouZer Jun 29 '21 at 11:10
  • 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. – Amir Jun 29 '21 at 11:12
  • The error is that it's trying to find the users table on the default connection database instead on the tenant's database. – HouZer Jun 29 '21 at 11:16
  • Hi @HouZer did find away to get the auth to use the tenant db ?? – Samer Ajlawi Apr 09 '23 at 14:28