Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ktc_tech's avatar

Console command to seed multi-database multi-tenant system

Hello,

We are working on a multi-database multi-tenant system and are preferring to write some console commands to loop through our tenants and run things like migrations or seed scripts utilizing the existing Artisan commands available (since they provide the --database and --path options).

We have an observer on our Tenant model so that when a Tenant is "created", a schema is created for them and their database is migrated.

We have a console command that lets us supply a tenant ID or "all" and it will run the appropriate migrations on the specified tenants, utilizing their respective database connection & migration paths.

When we run the command via command line, it works perfectly.

To make this more succinct, we have:

php artisan tenants:migrate (--tenants=) which effectively loops through supplied tenants and runs: php artisan migrate --database=(their connection name) --path=(tenant migration path)

When we run:

php artisan migrate:refresh --seed

The shared database is migrated perfectly and some initial Tenants are seeded.

Remember, when Tenants are created, an observer will dispatch a job to run:

php artisan tenants:migrate --tenants=(ther newly created ID)

Which in turn runs:

php artisan migrate --database=(their connection) --path=(migration path)

For some reason, it's attempting to run DatabaseSeeder on the tenant connections as well.

Anyone know why this is happening?

0 likes
0 replies

Please or to participate in this conversation.