Seeder runs fine the first time but does not do some tasks in the subsequent loops in Laravel 5?

前端 未结 1 1673
别那么骄傲
别那么骄傲 2021-01-21 21:30

I am working on a multi tenant multi database application where I have a main DB which consists of a tenant table and then for each tenant that is created, there is

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-21 21:50

    The database connection is kept alive in Laravel, which means that it will not switch to new database when you change the database name via config. You need to force the background connection to be reconnected by DB::connection($connection)->reconnect().

    When running migrations, because Laravel use the database name and the table name to check the existence of the migrations table, you need to update the table name of the current connection as well.

    In your case:

    # ...
    \Config::set('database.connections.tenant.database', $username);
    \Config::set('database.default', 'tenant');
    
    \DB::reconnect();
    \DB::setDatabaseName($username);
    # ...
    

    0 讨论(0)
提交回复
热议问题