multi-tenant icon indicating copy to clipboard operation
multi-tenant copied to clipboard

Active tenant isn't available in executed migrations

Open sasokovacic opened this issue 3 years ago • 3 comments

I can't get access to the active tenant (& db connection) when executing PHP artisan tenancy:migrate. I need to change some data after migrating some structural changes


Information

  • hyn/multi-tenant version: 5.4
  • laravel version: 5.8
  • database driver and version: MySQL 8.0.19
  • webserver software and version: Apache/2.4.41
  • php version: 7.4.2

sasokovacic avatar May 09 '21 07:05 sasokovacic

@sasokovacic Instead of running php artisan tenancy:migrate, please run php artisan tenancy:run migrate. Then you should be able to retrieve the current tenant in the migrations using app(\Hyn\Tenancy\Environment::class)->tenant();.

nbayramberdiyev avatar Jul 06 '21 12:07 nbayramberdiyev

@sasokovacic For the record there is another solution if you do not want to change your script and continue with tenancy:migrate. Please note this solution kind of a band-aid.

In your migration you can identify the tenant yourself by putting the following code in your migration:

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $environment = app(\Hyn\Tenancy\Environment::class);
        $tenantUuid = DB::connection()->getDatabaseName();
        $website = \Hyn\Tenancy\Models\Website::where('uuid', $tenantUuid)->firstOrFail();
        $environment->tenant($website);
       // Tenant is now identified like in a request 
    }

stein-j avatar Jul 25 '21 18:07 stein-j

@stein-j Thanks. I'll try your solution, but it would stilll be really useful to have this feature built into the package.

sasokovacic avatar Aug 15 '21 17:08 sasokovacic