Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Multitenancy Laravel Package

spatie/laravel-multitenancy

Unopinionated multitenancy for Laravel. Detect the current tenant per request and run configurable tasks when switching tenants. Supports single or multiple databases, tenant-aware queued jobs, per-tenant Artisan commands, and easy model connection handling.

View on GitHub
Deep Wiki
Context7

title: Switching route cache paths weight: 3

Laravel comes with route caching out of the box. By default all routes are cached, which means that the application will only load the routes once. This is great if your routes are static. However, if you're using dynamic routes, for example different routes for different tenants, you'll need to keep a separate route cache for each tenant.

The Spatie\Multitenancy\Tasks\SwitchRouteCacheTask can switch the configured APP_ROUTES_CACHE environment variable to a tenant specific value.

To use this task, you should uncomment it in the switch_tenant_tasks section of the multitenancy config file.

// in config/multitenancy.php

'switch_tenant_tasks' => [
    \Spatie\Multitenancy\Tasks\SwitchRouteCacheTask::class,
    // other tasks
],

A route cache for each tenant

In the default scenario, all tenants have different routes. The package creates a route cache file for each tenant: bootstrap/cache/routes-v7-tenant-{$tenant->id}.php.

Most importantly, you should use php artisan tenant:artisan route:cache to cache your routes instead of Laravel's default route:cache command. This will make sure a different route cache file is generated for each tenant.

Route cache shared across the tenants

It's the scenario where all tenants use the same routes. The package creates a shared route cache file for all tenants: bootstrap/cache/routes-v7-tenants.php.

To enable the feature you should set to true the shared_routes_cache section of the multitenancy config file.

// in config/multitenancy.php

'shared_routes_cache' => true,

Most importantly, you should use php artisan tenant:artisan route:cache --tenant=YOUR-TENANT-ID to cache your routes instead of Laravel's default route:cache command. This will make sure a different route cache file is generated for each tenant.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony