7

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?

4
  • If you could use the shared db multi tenant pattern some work has been done for laravel. github.com/AuraEQ/laravel-multi-tenant Commented Oct 18, 2015 at 5:57
  • @user993553 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+ Commented Oct 18, 2015 at 6:22
  • Found another one github.com/orchestral/tenanti works with 5 Commented Oct 18, 2015 at 6:32
  • actually i had developed much dont want to integrate other package so just asking is it best way. I Commented Oct 18, 2015 at 7:40

1 Answer 1

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,
        ],

    ],
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for you reply i did the same @mikel. i forgot to give answer here. Hope it will help other.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.