I am creating a multi-tenant application in which, based on the sub-domain, I am connecting to a database of that particular tenant.
Here is code to do that:
// To connect with a subdomain - the entry will be in config/database.php.
public static function connectSubdomainDatabase($dbname)
{
$res = DB::select("show databases like '{$dbname}'");
if (count($res) == 0) {
App::abort(404);
}
Config::set('database.connections.subdomain.database', $dbname);
//If you want to use query builder without having to specify the connection
Config::set('database.default', 'subdomain');
DB::reconnect('subdomain');
}
Is it the best way to connect with a database or is there any problem that because I am thinking from the performance point of view because every time I am connecting with the database when there are different subdomains. What is the best possible way to do that?