Reputation: 373
I used to fetch data from db and make available in all View through ServiceProvider, But now, I am using stancl/tenancy for multi-tenant system. And I need to pass some global data to all view files, writing the fetch data in ServiceProvider returns data from central database only. So, how can I fetch data from tenant DB to all view files globally before requesting any other requests?
Upvotes: 0
Views: 1484
Reputation: 398
Service Providers run before the tenant is identified and can therefore not be used to make configurations like sharing data to all views.
Instead, you can make a custom tenancy bootstrapper by creating a class that implements the Stancl\Tenancy\Contracts\TenancyBootstrapper
interface.
namespace App;
use View;
use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
class ViewBootstrapper implements TenancyBootstrapper
{
public function bootstrap(Tenant $tenant)
{
// Write your logic here.
View::share('variable', 'data');
}
public function revert()
{
// Optional, but recommended:
// Write you logic here that reverts the actions.
}
}
Finally, add it to the bootstrappers
array in your config/tenancy.php
file to enable the bootstrapper.
Upvotes: 2