Skip to content

Console commands

Tenancy comes with a number of Artisan commands. Most of them belong to a specific feature and are documented on that feature’s page, so here we just list them with a short description.

This page also covers things that apply to commands in general, like selecting which tenants a command runs for, running any command for tenants using tenants:run, and making your own commands tenant-aware.

CommandDescription
tenancy:installPublishes the config, the routes/tenant.php file, the TenancyServiceProvider, and the central migrations, and creates the database/migrations/tenant folder. See Getting started.
tenants:migrateRuns tenant migrations. See Migrations.
tenants:rollbackRolls back tenant migrations. See Migrations.
tenants:migrate-freshDrops all tables in tenant databases and runs tenant migrations again. See Migrations.
tenants:seedSeeds tenant databases. See Migrations.
tenants:dumpDumps the schema of a tenant database, squashing tenant migrations. See Migrations.
tenants:runRuns any Artisan command for tenants. See below.
tenant:tinkerStarts Tinker in the context of a tenant. See below.
tenants:listLists tenants and their domains. See below.
tenants:down, tenants:upEnables and disables maintenance mode for tenants. See Maintenance mode.
tenants:linkCreates (or, with --remove, removes) the public symlinks of tenant disks. See Filesystem bootstrapper.
tenants:pending-create, tenants:pending-clearCreates pending tenants until there’s a given number of them, and deletes pending tenants (optionally only those older than a given age). See Pending tenants.
tenants:rlsCreates the PostgreSQL RLS user and policies. See PostgreSQL RLS.
tenants:purge-impersonation-tokensDeletes expired impersonation tokens. See User impersonation.

Note that tenant:tinker is singular, since it runs for a single tenant. All other commands that work with tenants use the tenants: prefix.

Commands that run for multiple tenants run for all tenants by default. To run them for specific tenants only, pass tenant keys in the --tenants option. To run them for all tenants except some, use --skip-tenants. Both options can be repeated:

Terminal window
php artisan tenants:migrate --tenants=foo --tenants=bar
php artisan tenants:migrate --skip-tenants=foo

When calling these commands from PHP code, pass the tenant keys as an array:

Artisan::call('tenants:migrate', [
'--tenants' => ['foo', 'bar'],
]);

These commands also accept --with-pending. If you use pending tenants, it overrides the tenancy.pending.include_in_queries config for that run. See the Pending tenants page for details.

These options are accepted by tenants:migrate, tenants:rollback, tenants:migrate-fresh, tenants:seed, tenants:run, tenants:down, tenants:up, and tenants:link, as well as by any of your own commands that use the HasTenantOptions trait.

To limit which tenants a command runs for regardless of the options passed to it, set the command’s $scopeTenantsQuery static property to a closure that scopes the tenants query:

app/Providers/TenancyServiceProvider.php
use Illuminate\Database\Eloquent\Builder;
use Stancl\Tenancy\Commands\Migrate;
Migrate::$scopeTenantsQuery = fn (Builder $query) => $query->where('region', config('app.region'));

Each command has its own scope, so the example above only affects tenants:migrate. The scope is combined with the options above, so e.g. --tenants can only select tenants that match the scope.

The MigrateDatabase and SeedDatabase jobs run for a specific tenant, so they ignore the scopes of tenants:migrate and tenants:seed.

The tenants:run command lets you run any Artisan command, including your own, for tenants. For each tenant, it initializes tenancy and then runs the command.

The command is passed as a single string, along with its arguments and options. For example, if you have an email:send {body} {--queue} {--subject=} command:

Terminal window
php artisan tenants:run "email:send --queue --subject='New feature' 'We have launched a new feature.'"

Arguments and options that contain spaces need to be quoted inside the string, just like on the command line. Options like --tenants are passed to tenants:run itself, so they go outside the string:

Terminal window
php artisan tenants:run "email:send 'We have launched a new feature.'" --tenants=foo --tenants=bar

If the command asks questions (e.g. using $this->ask()), it prompts you for each tenant.

If the command fails for some tenant (returns a non-zero exit code), tenants:run still runs it for the remaining tenants and then returns the exit code of the first failure.

When calling tenants:run using Artisan::call(), the command string goes in the commandname argument, and --tenants (or --skip-tenants) is an array:

Artisan::call('tenants:run', [
'commandname' => "email:send --subject='New feature' 'We have launched a new feature.'",
'--tenants' => ['foo', 'bar'],
]);

Artisan::output() then contains the output of the command for each tenant, along with the Tenant: ... lines printed by tenants:run.

tenants:run lets you run any command for tenants, but if a command should always run for tenants, you can make it tenant-aware instead. The package provides two traits for this:

  • Stancl\Tenancy\Concerns\TenantAwareCommand runs the command’s handle() (or __invoke()) method once for each tenant returned by the getTenants() method, with tenancy initialized for that tenant.
  • Stancl\Tenancy\Concerns\HasTenantOptions adds the --tenants, --skip-tenants, and --with-pending options to the command, and implements getTenants() based on them.

When you use both traits, the command runs for all tenants by default and accepts the same options as the built-in commands. You don’t need to change anything in handle():

app/Console/Commands/SendNewsletter.php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Stancl\Tenancy\Concerns\HasTenantOptions;
use Stancl\Tenancy\Concerns\TenantAwareCommand;
class SendNewsletter extends Command
{
use TenantAwareCommand, HasTenantOptions;
protected $signature = 'newsletter:send';
public function handle()
{
// Runs in the context of each tenant
}
}
Terminal window
php artisan newsletter:send
php artisan newsletter:send --tenants=foo

If handle() returns a non-zero exit code for any tenant, the command still runs for the remaining tenants and then returns a non-zero exit code. $this->fail(), on the other hand, stops the command right away, so it doesn’t run for the remaining tenants.

Isolatable commands work too. With --isolated, the whole command is locked rather than each tenant separately, so it won’t run for any tenant if another instance is already running.

php artisan tenant:tinker starts a Tinker session with tenancy initialized for a tenant. You can pass a tenant key as an argument:

Terminal window
php artisan tenant:tinker foo

Without the argument, the command lets you pick the first tenant, or search for a tenant by its key or domain.

php artisan tenants:list lists tenant keys, along with the domains of each tenant.