phplaraveldatabasemulti-tenantlaravel-artisan

Laravel stancl/tenancy: Tenant migrations "Nothing to migrate" (separate tenant DBs, migrations in database/migrations/tenant)


I am running tenant new migration (orders_table migration) that live in database/migrations/tenant/ . Each tenant has its own database (tenant_1, tenant_2, tenant_3, ...) enter image description here

and there are some tables inside already. The central database is separate database called inventory.

when i run migrations using php artisan tenants:migrate, it do migrations on directory database/migrations/tenant/ enter image description here

but this migrations are done on my central database not on tenant 1 database.For tenant_2, tenant_3, .... it says ā€œNothing to migrate.ā€ The new table (orders) is not created in those tenant DBs. And new migrations are on migration/tenant folder

i have tried to checked config/tenancy.php includes the tenant migration path:

'migration_parameters' => [
        '--force' => true, // This needs to be true to run migrations in production.
        '--path' => [database_path('migrations/tenant')],
        '--realpath' => true,
    ],

when i run php artisan tenants:list it show perfectly real total of tenants

I am using laravel 12 and stancl/tenancy: ^3.8


Solution

  • after struggle with config/tenancy.php and config/database.php configuration still the migration occired only on central database as tenant 1 and for rest said nothing to migrate but after adding the following line of code to bootstrap/providers.php

    
    return [
        App\Providers\AppServiceProvider::class,
        App\Providers\TenancyServiceProvider::class,   // <-- this is important also make sure it available
    ];
     
    

    and Make sure you Tenant Model implements TenantWithDatabase

    <?php
    
    namespace App\Tenancy;
    
    use Stancl\Tenancy\Database\Models\Tenant as StanclTenant;
    use Stancl\Tenancy\Contracts\TenantWithDatabase;
    use Stancl\Tenancy\Database\Concerns\HasDatabase;
    use Stancl\Tenancy\Database\Concerns\HasDomains;
    
    class BaseTenant extends StanclTenant implements TenantWithDatabase
    {
        use HasDatabase, HasDomains;
    
        protected $fillable = [
            'company_name',
            'database',
            'address',
            'data',
        ];
    
        public function getDatabaseName(): string
        {
            return $this->database;
        }
    }
    
    

    in config/tenancy.php

    use App\Tenancy\BaseTenant; //replace with above file location
    
    return [
        'tenant_model' => BaseTenant::class,
    :
    :