How can I handle subdomains with one laravel installation How can I handle subdomains with one laravel installation laravel laravel

How can I handle subdomains with one laravel installation


Laravel supports multiple Database connections. Firstly, define the connections in config/database.php:

<?phpreturn array(    'default' => 'default_connection',    'connections' => array(        // domain.com        'default_connection' => array(            'driver'    => 'mysql',            'host'      => 'localhost',            'database'  => 'primary_database',            'username'  => 'username',            'password'  => 'password'            'charset'   => 'utf8',            'collation' => 'utf8_unicode_ci',            'prefix'    => '',        ),        // sub.domain.com        'subdomain_connection' => array(            'driver'    => 'mysql',            'host'      => 'localhost',            'database'  => 'secondary_database',            'username'  => 'username',            'password'  => 'password'            'charset'   => 'utf8',            'collation' => 'utf8_unicode_ci',            'prefix'    => '',        ),    ),);

Now to specify which connection your models should use you can set the $connection property in your models:

<?phpclass YourModel extends Eloquent {    protected $connection = 'subdomain_connection';}

You can set the value of $connection programatically.


Multi-tenancy is a tricky architecture that needs care to model. There are several ways to achieve this architecture. Some decide to use a single database whiles others prefer to use multiple database(that is in your case).

They both have their pros and cons that you need to consider. There are a lot of factors that need to be taken into consideration before you start modeling your application. eg Virtual host configuration for subdomains, Database migration(rollback all databases when the need be, etc.). I will suggest these two packages that can help you going and give you more insight on how to model your application to suite what you want.

https://github.com/orchestral/tenanti

https://github.com/hyn/multi-tenant


I would do that way:

  • Create one database per domain

  • Set up the available DB connections in laravel config/database.php :

'connections' => [     'mysql_domain_1' => [        'driver'    => 'mysql',        /* other config values... */    ],    'mysql_domain_2' => [        'driver'    => 'mysql',        /* other config values... */    ]];
  • In the early fase of the request cycle (for example in a Middleware), get the sub-domain from the request, and set the current DB connection accordingly

    For example create a middleware and in the handle method:

public function handle($request, Closure $next){    //check the request URL and get subdomain    //get the db connection associated to the subdomain     //set the connection for this request    Config::set('database.default', $dbConnection);} 

Config::set('database.default', $dbConnection ); will set the db connection used by the whole application for the current request cycle