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: Ensuring a current tenant has been set weight: 1

In your project you probably will have many routes where you expect a tenant has been made current.

You can ensure that a current tenant has been set by applying the \Spatie\Multitenancy\Http\Middleware\NeedsTenant middleware on those routes.

We recommend registering this middleware in a group alongside \Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession, to also verify that the session is not being abused across multiple tenants.

// in `app\Http\Kernel.php`

protected $middlewareGroups = [
    // ...
    'tenant' => [
        \Spatie\Multitenancy\Http\Middleware\NeedsTenant::class,
        \Spatie\Multitenancy\Http\Middleware\EnsureValidTenantSession::class
    ]
];

With the middleware registered, you can use it in routes files (or in a route service provider).

// in a routes file

Route::middleware('tenant')->group(function() {
    // routes
})

If the request does not have a "current" tenant for these routes, an Spatie\Multitenancy\Exceptions\NoCurrentTenant exception will be thrown. You can listen for that exception in the exception handler. You could set some kind of flash message and redirect to a login page there.

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.
calmfox/watch-sylius
damienfern/grpc-symfony-bundle
atoolo/index-bundle
atoolo/genai-bundle
coprotoai/laravel-ticket
davidjln/llm-carbon-bundle
cryonighter/valid-request-bundle
coolms/taxonomy-bundle
coolms/field-bundle
articulate-orm/symfony
aaix/laravel-tall-architect
ephoto/akeneo-connector
emmanuelballery/eb-plantumlbundle
emielburgman/symfony-visitor-beacon
emielburgman/symfony-visit-storage
emielburgman/symfony-security-headers
emielburgman/symfony-log-viewer
emarref/xdebug-bundle
emarref/pubnub-bundle
elriseio/finance-money-bundle