Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

gauravchandiramani's avatar

Using Auth on a different connection or database.

I'm very new to Laravel and am currently building a multi-tenant app. Basically when anyone signs up for an account a new database along with tables, indexes and data gets created. I've managed to create these tables and also managed to switch dynamically between the default database and the account database to retrieve and insert data. I use Config::set() to basically switch between databases on the fly.

I'm currently stuck with user authentication. User signup works fine and all user credentials are stored in their respective account databases. So let's say account ABC has 3 users. Their email and password will be in "databaseabc". When a user wants to login, Auth should basically switch to the accountdb and check on the users table there (the default database has no users table). Passing parameters (database name, database username, database password) to AuthController is not a problem. I just need to know how to switch to it using the account database instead of the default one. I tried using Config::set() in AuthController but that didn't help.

Please let me know if anyone has any ideas about this.

Thank you.

0 likes
10 replies
pixelpeter's avatar

In your model you can define which connection to use with:

protected $connection = 'my-other-connection';

Now you just need to set it dynamically in your application based on your criteria.

I didn't tried it (dynamically) but I think this should solve your problem

gauravchandiramani's avatar

Hi @pixelpeter, thanks for your reply.

So I added the connection variable to my User Model. I also wrote the following function to dynamically change the connection -

public function changeConnection($conn){
    $this->connection = $conn;
}

Now the question is how do I call this function when trying Auth::attempt ?

Please do let me know. Thank you.

1 like
juan's avatar

Hello there, were you able to figure this out? I am actually stuck on the same part. I also created a function to change the connection dynamically.

Jonny87's avatar

I have the same problem! How can i pass data from auth::attempt to app/user ? I need the connection name in the app/user contructor, which is given in user database column.

ewliang's avatar

I added a connection line declaration in constructor of my authcontroller. The line is: DB::setDefaultConnection('yourConnectionName'); Don't forget to add "use DB;" (without quotes ofc) at the top of the file ;).

1 like
molerat's avatar

Using Laravel 5 what would be the "AuthController"?

slightlyprivate's avatar

This post helped me to figure out how to use Passport for my API, while having different databases for subdomain accounts. In my application, each valid subdomain has its own database with a users table. I have two User models: DefaultUser and AccountUser which extend App\User. My App\User model has the changeConnection function and I call that function in a CustomUserProvider class, which is used by a CustomAuthProvider class, and in my config/auth.php I have:

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'default_api' => [
            'driver' => 'passport',
            'provider' => 'default_users',
        ],

        'account_api' => [
            'driver' => 'passport',
            'provider' => 'account_users',
        ],
    ],

...

    'providers' => [
        'default_users' => [
            'driver' => 'custom',
            'model' => App\Models\DefaultUser::class,
        ],
        'account_users' => [
            'driver' => 'custom',
            'model' => App\Models\AccountUser::class,
        ],                        
        'users' => [
            'driver' => 'custom',
            'model' => App\User::class,
        ],

It appears to be working for me so far, so I am moving on. Hopefully this helps anyone who gets here as I did.

shoffstall's avatar

@matthall00 can you share what the other two models look like please? I am struggling with the same issue and everything works fine but oauth is calling the central dB instead of the tenant dB and seems to be the only thing ignoring my middleware.

naingarkarwin's avatar

This is for auth.php We need to define our custom guard when we use that guard. And provider is also created because we need to use it in our new guard

'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
        'guard_one' => [
            'driver' => 'session',
            'provider' => 'connection_one_users'
        ]
],
'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        'connection_one_users' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],
],

You need to extends Authenticatable instead of models in your model class. The model used in new provider will be like

use Illuminate\Foundation\Auth\User as Authenticatable;

class Admin extends Authenticatable
{
    protected $connection = 'connection_one';
}
kh_dev1's avatar

Hello sir you working now ? I need like you also can you share your source code?

Please or to participate in this conversation.