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.
Built-in commands
Section titled “Built-in commands”| Command | Description |
|---|---|
tenancy:install | Publishes the config, the routes/tenant.php file, the TenancyServiceProvider, and the central migrations, and creates the database/migrations/tenant folder. See Getting started. |
tenants:migrate | Runs tenant migrations. See Migrations. |
tenants:rollback | Rolls back tenant migrations. See Migrations. |
tenants:migrate-fresh | Drops all tables in tenant databases and runs tenant migrations again. See Migrations. |
tenants:seed | Seeds tenant databases. See Migrations. |
tenants:dump | Dumps the schema of a tenant database, squashing tenant migrations. See Migrations. |
tenants:run | Runs any Artisan command for tenants. See below. |
tenant:tinker | Starts Tinker in the context of a tenant. See below. |
tenants:list | Lists tenants and their domains. See below. |
tenants:down, tenants:up | Enables and disables maintenance mode for tenants. See Maintenance mode. |
tenants:link | Creates (or, with --remove, removes) the public symlinks of tenant disks. See Filesystem bootstrapper. |
tenants:pending-create, tenants:pending-clear | Creates 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:rls | Creates the PostgreSQL RLS user and policies. See PostgreSQL RLS. |
tenants:purge-impersonation-tokens | Deletes 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.
Selecting tenants
Section titled “Selecting tenants”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:
php artisan tenants:migrate --tenants=foo --tenants=barphp artisan tenants:migrate --skip-tenants=fooWhen 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.
Scoping the tenants query
Section titled “Scoping the tenants query”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:
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.
Running commands for tenants
Section titled “Running commands for tenants”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:
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:
php artisan tenants:run "email:send 'We have launched a new feature.'" --tenants=foo --tenants=barIf 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.
Calling tenants:run from PHP code
Section titled “Calling tenants:run from PHP code”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.
Writing tenant-aware commands
Section titled “Writing tenant-aware commands”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\TenantAwareCommandruns the command’shandle()(or__invoke()) method once for each tenant returned by thegetTenants()method, with tenancy initialized for that tenant.Stancl\Tenancy\Concerns\HasTenantOptionsadds the--tenants,--skip-tenants, and--with-pendingoptions to the command, and implementsgetTenants()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():
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 }}php artisan newsletter:sendphp artisan newsletter:send --tenants=fooIf 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.
Tinker
Section titled “Tinker”php artisan tenant:tinker starts a Tinker session with tenancy initialized for a tenant. You can pass a tenant key
as an argument:
php artisan tenant:tinker fooWithout the argument, the command lets you pick the first tenant, or search for a tenant by its key or domain.
Listing tenants
Section titled “Listing tenants”php artisan tenants:list lists tenant keys, along with the domains of each tenant.