Making Artisan command tenant aware | laravel-multitenancy | Spatie

 SPATIE

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

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Laravel-multitenancy](https://spatie.be/docs/laravel-multitenancy/v4)  Advanced-usage  Making Artisan command tenant aware

 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)

  Making Artisan command tenant aware
- [ Introduction ](https://spatie.be/docs/laravel-multitenancy/v4/introduction)
- [ Upgrade guide ](https://spatie.be/docs/laravel-multitenancy/v4/upgrade-guide)
- [ Support us ](https://spatie.be/docs/laravel-multitenancy/v4/support-us)
- [ Requirements ](https://spatie.be/docs/laravel-multitenancy/v4/requirements)
- [ Questions and issues ](https://spatie.be/docs/laravel-multitenancy/v4/questions-issues)
- [ Changelog ](https://spatie.be/docs/laravel-multitenancy/v4/changelog)
- [ ](https://spatie.be/docs/laravel-multitenancy/v4/installation-setup)

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

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

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

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

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

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

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

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

 Making Artisan command tenant aware
===================================

###  On this page

1. [ Using the tenants:artisan command ](#content-using-the-tenantsartisan-command)

Commands can be made tenant aware by applying the `TenantAware` trait. When using the trait it is required to append `{--tenant=*}` or `{--tenant=}` to the command signature.

Caution: If you append `{--tenant=*}`, then if no `tenant` option is provided when executing the command, the command will execute for *all* tenants.

```
use Illuminate\Console\Command;
use Spatie\Multitenancy\Commands\Concerns\TenantAware;

class YourFavoriteCommand extends Command
{
    use TenantAware;

    protected $signature = 'your-favorite-command {--tenant=*}';

    public function handle()
    {
        return $this->line('The tenant is '. Tenant::current()->name);
    }
}
```

When executing the command, the `handle` method will be called for each tenant.

```
php artisan your-favorite-command
```

Using the example above, the name of each tenant will be written to the output of the command.

You can also execute the command for a specific tenant:

```
php artisan your-favorite-command --tenant=1
```

Using the tenants:artisan command
---------------------------------------------------------------------------------------------------------------------------------------------------------

If you cannot change an Artisan command yourself, for instance a command from Laravel itself or a command from a package, you can use `tenants:artisan `. This command will loop over tenants and for each of them make that tenant current, and execute the artisan command.

When your tenants each have their own database, you could migrate each tenant database with this command (given you are using a task like [`SwitchTenantDatabase`](https://docs.spatie.be/laravel-multitenancy/v4/using-tasks-to-prepare-the-environment/switching-databases)):

```
php artisan tenants:artisan migrate
```

We are using the `migrate` command here, but you can pass any command that you like.

### Passing arguments and options

If you use quotes around the command part you can use any argument and option that the command supports.

```
php artisan tenants:artisan "migrate --seed"
```

### Running artisan command for specific tenants

If the command only needs to run for a specific tenant, you can pass its `id` to the `tenant` option.

```
php artisan tenants:artisan "migrate --seed" --tenant=123
```

### State Persistence

When using `TenantAware`, the same command instance is executed for each tenant. This means that instance properties will retain their values between tenant executions unless explicitly reset.

```
use Illuminate\Console\Command;
use Spatie\Multitenancy\Commands\Concerns\TenantAware;

class YourFavoriteCommand extends Command
{
use TenantAware;

    protected $signature = 'your-favorite-command {--tenant=*}';

    protected int $counter = 0;

    public function handle()
    {
        // Reset state at the beginning of each tenant execution
        $this->counter = 0;

        // Without the reset above, $counter would start at the value
        // from the previous tenant's execution
        $this->incrementCounter();

        return $this->line('Counter: '. $this->counter);
    }

    public function incrementCounter()
    {
        $this->counter++;
    }
}
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
