Multiple Schema based on Laravel 5.1 subdomain

Asked

Viewed 79 times

2

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

  1. client closes the contract;
  2. within a Wizzard, he selects that his subdomain will be foo;
  3. then I will create the bank with the same subdomain foo.myapp.;
  4. From this, Laravel must find such a bank for such a domain

Is there any way I can define the access bank based on the subdomain?

  • I made it very short buddy. It’s late! ;)

  • Hello, take a look at this link (English), if I understand your problem well it will help you to create this dynamism without having to create a model whenever a customer arises. https://lukevers.com/2015/03/25/on-the-fly-database-connections-with-laravel-5/

1 answer

1

In the basement, in the archive config/database.php you can define more than one connection with 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 that client you could set the connection like this:

class TabelaModel extends Model
{
     protected $connection = 'mysql_2';
}
  • but so I would always have to define a specific model for this client. my logic would be to have "n" databases for the same application, but for each subdomain => 1 database

  • I have the same problem, you managed to solve?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.