samedi 17 octobre 2015

Best Way to Connect Multiple Database in Laravel

I am creating Multi-tenant Application in which based on the sub-domain i am connecting to database of that particular Tenant. Here is code to do that :

//  To Connect with Subdomain - 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 best way to connect with database or there any problem that because i am thinking in performance point of view because every time i am connecting with the database when there different subdomains. Please suggest best possible way to that.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire