Multiplo Schema based on subdomain Laravel 5.1

2

I am creating an application that, when the client is creating the structure of his store, he will get a subdomain (which he will choose himself) and the database that Laravel will fetch will have the name of the subdomain. EX:

  • customer closes the contract;
  • Within a wizzard, it selects that your subdomain will be foo ;
  • Next I will create the bank with the same subdomain foo .myapp.com;
  • From this, Laravel should fetch such a bank for such a domain
  • Is there any way I can set the access database based on the subdomain?

        
    asked by anonymous 02.12.2015 / 01:51

    1 answer

    1

    In laravel, in file config/database.php you can define more than one connection to banks

    Example:

      'default' => 'mysql_1',
      'mysql_1' => [
          ...
      ],
      'mysql_2' => [
         ...
      ]
    

    On the routes you will do something like:

    Route::group(array('domain' => '{account}.myapp.com'), function()
    {
        // Wallace é o $account
        Route::get('user/{id}', function($account, $id)
        {
            App\Wallace\TabelaModel::find($id);
        });
    
    });
    

    Then you could have the client namespace. In the models of this client you could define the connection like this:

    class TabelaModel extends Model
    {
         protected $connection = 'mysql_2';
    }
    
        
    02.12.2015 / 02:15