1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 16:14:03 +00:00

Delete tenants on migrate:fresh

This commit is contained in:
lukinovec 2022-10-10 17:38:38 +02:00
parent 6d303d1b2e
commit 05e84401b4

View file

@ -13,6 +13,9 @@ use Stancl\Tenancy\Contracts\Tenant;
use Stancl\Tenancy\Enums\LogMode;
use Stancl\Tenancy\Events\Contracts\TenancyEvent;
use Stancl\Tenancy\Resolvers\DomainTenantResolver;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Support\Facades\Schema;
use Stancl\Tenancy\Database\Contracts\TenantWithDatabase;
class TenancyServiceProvider extends ServiceProvider
{
@ -126,6 +129,22 @@ class TenancyServiceProvider extends ServiceProvider
}
});
Event::listen(CommandStarting::class, function (CommandStarting $event) {
$tenantModel = tenancy()->model();
if ($event->command === 'migrate:fresh' && Schema::hasTable($tenantModel->getTable())) {
$tenantModel::all()->each(function(Tenant $tenant) {
if (method_exists($tenant, 'domains')) {
$tenant->domains()->delete();
}
if ($tenant instanceof TenantWithDatabase) {
$tenant->delete();
}
});
}
});
$this->app->singleton('globalUrl', function ($app) {
if ($app->bound(FilesystemTenancyBootstrapper::class)) {
$instance = clone $app['url'];