Active tenant isn't available in executed migrations
sasokovacic opened this issue · 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 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();
.
@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 Thanks. I'll try your solution, but it would stilll be really useful to have this feature built into the package.