Best way to connect multiple databases in Laravel [duplicate]
Asked Answered
J

1

6

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?

Jeremy answered 18/10, 2015 at 5:52 Comment(4)
If you could use the shared db multi tenant pattern some work has been done for laravel. github.com/AuraEQ/laravel-multi-tenantClearance
@Clearance Yes i checked that before but as i have mentioned in my tag i want it for Laravel-5.1 and package is of 4.2+Jeremy
Found another one github.com/orchestral/tenanti works with 5Clearance
actually i had developed much dont want to integrate other package so just asking is it best way. IJeremy
C
1

This is nearly the best way to do this. In the end, it's all opinion anyway. However, I would create a connection in the configuration file for each of the subdomains. Then, in your connectSubdomainDatabase() function, I would get the current subdomain instead of passing a database name. You can already specify a connection in laravel, the only place you should be using database names is in the config file.

So, something like this:

// To connect with a subdomain - the entry will be in config/database.php.
public static function connectSubdomainDatabase()
{
    // Break apart host
    $urlParts = explode('.', $_SERVER['HTTP_HOST']);

    // Change default connection
    Config::set('database.default', $urlParts[0]);
 }

Where the config/database.php connections is:

'connections' => [

        'subdomain1' => [
            'driver'    => 'mysql',
            'host'      => env('DB_HOST', 'localhost'),
            'database'  => env('DB_DATABASE', 'forge'),
            'username'  => env('DB_USERNAME', 'forge'),
            'password'  => env('DB_PASSWORD', ''),
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
            'strict'    => false,
        ],

        'subdomain2' => [
                'driver'    => 'mysql',
                'host'      => env('DB_HOST', 'localhost'),
                'database'  => env('DB_DATABASE', 'forge'),
                'username'  => env('DB_USERNAME', 'forge'),
                'password'  => env('DB_PASSWORD', ''),
                'charset'   => 'utf8',
                'collation' => 'utf8_unicode_ci',
                'prefix'    => '',
                'strict'    => false,
        ],

    ],
Chloras answered 5/11, 2015 at 20:58 Comment(1)
Thank you for you reply i did the same @mikel. i forgot to give answer here. Hope it will help other.Jeremy

© 2022 - 2024 — McMap. All rights reserved.