laravel-multidomain
laravel-multidomain copied to clipboard
Models
How should I use this tool if I have an application that shares the same database on all domains?
For example,
If i have this tables
Products Settings Menus
Would I need to add the domain as an id in each model and add a global scope to filter by domain?
Hi,
yes this is a possible solution.
Other solutions could be
- use distinct tables for each domain, setting them in the model class depending upon the domain.
- use two separate dbs: one centralized db with the data shared across all the domains, and one specific db for each domain containing only the domain specific tables (this is a solution which I used in various settings when I have centralized and localized data but obviously building the queries is a bit more complicated) Cheers
Giacomo
Okay, my approach is:
-
Each domain, separate DB ==> details are fed into .env.domain.test file
-
For domain-specific details a. I created a config file - home-blocks.php Nothing but a json array ==> 'domain.test' => 'Blocks' 'Benefit Blocks' 'Featured Blocks' ..... (whatever data I want for that domain)
-
Then fetch the domain using getDomain() function in helpers.php and pass it to the controllers and views using AppServiceProvider:
view()->composer('*',function($view) { $view->with('domain', getDomain() ); });
Now, in the view or controller file, I just call those values like this in my controller:
public function index()
{
$domain=$this->domain;
$benefit_blocks=Config::get('home-blocks.'.$domain.'.blocks.benefit_blocks');
return view('home', compact( 'benefit_blocks' ));
}
As for the menu, I am using https://github.com/nWidart/laravel-menus/ and I just call my menu name which is the same as my domain name ...
{!! Menu::render( $domain ) !!}
Again, my laravel skills are pretty basic and I have no idea whether this is an efficient approach.
Hope it helps.