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?
Asked
Active
Viewed 1,111 times
1 Answers
2
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.

erikgaal
- 398
- 1
- 11
-
Thank you for the comment, But you didn't get my question. I want to fetch data from tenant database, not the central database. – amit Jun 09 '21 at 11:41
-
@amit I'm sorry, I just realised that too. I've updated my answers. :) – erikgaal Jun 09 '21 at 11:42