Creating your own task | laravel-multitenancy | Spatie

 SPATIE

  Laravel Multitenancy
=======================

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-multitenancy](https://spatie.be/docs/laravel-multitenancy/v1)  Using-tasks-to-prepare-the-environment  Creating your own task

 Version   v4   v3   v2   v1

 Other versions for crawler [v4](https://spatie.be/docs/laravel-multitenancy/v4) [v3](https://spatie.be/docs/laravel-multitenancy/v3) [v2](https://spatie.be/docs/laravel-multitenancy/v2) [v1](https://spatie.be/docs/laravel-multitenancy/v1)

- [ Introduction ](https://spatie.be/docs/laravel-multitenancy/v1/introduction)
- [ Support us ](https://spatie.be/docs/laravel-multitenancy/v1/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-multitenancy/v1/requirements)
- [ Questions and issues ](https://spatie.be/docs/laravel-multitenancy/v1/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-multitenancy/v1/changelog)
- [ ](https://spatie.be/docs/laravel-multitenancy/v1/installation-setup)

Installation
------------

- [ Base installation ](https://spatie.be/docs/laravel-multitenancy/v1/installation/base-installation)
- [ Using a single database ](https://spatie.be/docs/laravel-multitenancy/v1/installation/using-a-single-database)
- [ Using multiple databases ](https://spatie.be/docs/laravel-multitenancy/v1/installation/using-multiple-databases)
- [ Determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v1/installation/determining-current-tenant)

Basic usage
-----------

- [ Automatically determining the current tenant ](https://spatie.be/docs/laravel-multitenancy/v1/basic-usage/automatically-determining-the-current-tenant)
- [ Working with the current tenant ](https://spatie.be/docs/laravel-multitenancy/v1/basic-usage/working-with-the-current-tenant)
- [ Making queues tenant aware ](https://spatie.be/docs/laravel-multitenancy/v1/basic-usage/making-queues-tenant-aware)

Advanced usage
--------------

- [ Ensuring a current tenant has been set ](https://spatie.be/docs/laravel-multitenancy/v1/advanced-usage/ensuring-a-current-tenant-has-been-set)
- [ Looping over a collection of tenants ](https://spatie.be/docs/laravel-multitenancy/v1/advanced-usage/looping-over-a-collection-of-tenants)
- [ Making Artisan command tenant aware ](https://spatie.be/docs/laravel-multitenancy/v1/advanced-usage/making-artisan-commands-tenant-aware)
- [ Using a custom tenant model ](https://spatie.be/docs/laravel-multitenancy/v1/advanced-usage/using-a-custom-tenant-model)
- [ Listening for events ](https://spatie.be/docs/laravel-multitenancy/v1/advanced-usage/listening-for-events)
- [ Executing code for tenants and landlords ](https://spatie.be/docs/laravel-multitenancy/v1/advanced-usage/executing-code-for-tenants-and-landlords)

Using tasks to prepare the environment
--------------------------------------

- [ Overview ](https://spatie.be/docs/laravel-multitenancy/v1/using-tasks-to-prepare-the-environment/overview)
- [ Creating your own task ](https://spatie.be/docs/laravel-multitenancy/v1/using-tasks-to-prepare-the-environment/creating-your-own-task)
- [ Switching databases ](https://spatie.be/docs/laravel-multitenancy/v1/using-tasks-to-prepare-the-environment/switching-databases)
- [ Prefixing cache ](https://spatie.be/docs/laravel-multitenancy/v1/using-tasks-to-prepare-the-environment/prefixing-cache)

      You are viewing the documentation for **an older version** of this package. You can check the version you are using with the following command:

 `                                    composer show spatie/laravel-multitenancy                                                                                                                                                                                                                                    `

Creating your own task
======================

###  On this page

1. [ Registering a task ](#content-registering-a-task)
2. [ Accepting parameters ](#content-accepting-parameters)

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\Models\Tenant;

interface SwitchTenantTask
{
    public function makeCurrent(Tenant $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\Models\Tenant;

class PrefixCacheTask implements SwitchTenantTask
{
    protected ?string $originalPrefix;

    public function __construct()
    {
        $this->originalPrefix = config('cache.prefix');
    }

    public function makeCurrent(Tenant $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
    }
}
```
