php - Laravel tinker in multi-tenant environment -
i developing multi-tenant application using laravel-5.2 each tenant have separate database. each tenant has separate subdomain. detect tenants using subdomains.
i have setup models tenant
, databaseconnection
tenant hasone databaseconnection
, databaseconnection belongsto tenant
. db connections tenants set dynamically beforemiddleware
. these work well.
now want use artisan tinker
tenants. if run php artisan tinker
connects tenant
db credentials present in .env
file.
so trying make console command same. here's have achieved far.
class clienttinker extends command { protected $name = 'cdb:tinker'; public function fire() { // subdomain $subdomain = $this->argument('subdomain'); // client $client = tenant::wheresubdomain($subdomain)->first(); $connection = $client->databaseconnection(); // $connection contains database server, database name, user name, , password. // dynamically set connections here. *how?* ... // *i need call tinker here. how?* } protected function getarguments() { return [ ['subdomain', inputargument::required, 'subdomain of tenant.'], ]; }
so how set db connections specific tenant , how run tinker?
after $connection
variable in fire()
, this
db::purge('mysql'); config::set('database.connections.mysql.host', $connection->server); config::set('database.connections.mysql.database', $connection->database); config::set('database.connections.mysql.username', $connection->username); config::set('database.connections.mysql.password', $connection->password); // assuming variable names in $connection object here, have not specified them in question. artisan::call('tinker');
see if works you.
Comments
Post a Comment