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: Creating your own task weight: 2

A task is any class that implements Spatie\Multitenancy\Tasks\SwitchTenantTask. Here is how that interface looks like.

namespace Spatie\Multitenancy\Tasks;

use Spatie\Multitenancy\Contracts\IsTenant;

interface SwitchTenantTask
{
    public function makeCurrent(IsTenant $tenant): void;

    public function forgetCurrent(): void;
}

The makeCurrent function will be called when making a tenant current. A common thing to do would be to dynamically change some configuration values.

forgetCurrent will be called when forgetting a tenant. This function should restore the original environment. An important thing to note is that SwitchTenantTask are singletons, so you could store the original values as a property and reach for them later.

Here is an example implementation where we are going to use a prefix when a tenant is current, and clear out that prefix when forgetting the tenant.

namespace Spatie\Multitenancy\Tasks;

use Spatie\Multitenancy\Contracts\IsTenant;

class PrefixCacheTask implements SwitchTenantTask
{
    public function __construct(protected ?string $originalPrefix = null)
    {
        $this->originalPrefix ??= config('cache.prefix');
    }

    public function makeCurrent(IsTenant $tenant): void
    {
        $this->setCachePrefix("tenant_{$tenant->id}");
    }

    public function forgetCurrent(): void
    {
        $this->setCachePrefix($this->originalPrefix);
    }

    protected function setCachePrefix(string $prefix): void
    {
        config()->set('cache.prefix', $prefix);

        $storeName = config('cache.default');

        app('cache')->forgetDriver($storeName);
    }
}

Registering a task

After creating a task, you must register it by putting its class name in the switch_tenant_tasks key of the multitenancy config file.

Accepting parameters

Classes that implement SwitchTenantTask can accept parameters from the multitenancy config file.

'switch_tenant_tasks' => [
    \App\Support\SwitchTenantTasks\YourTask::class => ['name' => 'value', 'anotherName' => 'value'],
    // other tasks
],

In your task you can accept these parameters via the constructor. Make sure the parameter names matches those used in the config file.

namespace App\Support\SwitchTenantTasks\YourTask

use Spatie\Multitenancy\Tasks\SwitchTenantTask;

class SwitchTenantDatabaseTask implements SwitchTenantTask
{
    public function __construct(string $name, string $anotherName)
    {
        // do something
    }
}

You can also use the construct to inject dependencies. Just make sure the variable name does not conflict with one of the parameter names in the config file.

namespace App\Support\SwitchTenantTasks\YourTask

use Spatie\Multitenancy\Tasks\SwitchTenantTask;

class SwitchTenantDatabaseTask implements SwitchTenantTask
{
    public function __construct(string $name, string $anotherName, MyDepencency $myDependency)
    {
        // do something
    }
}
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