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 define what happens when switching tenants. Supports single or multiple databases, tenant-aware queued jobs, commands that run per tenant, and easy per-model connection setup.

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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport