Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Tasks/PrefixCacheTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Multitenancy\Tasks;

use Illuminate\Support\Facades\Cache;
use Spatie\Multitenancy\Models\Tenant;

class PrefixCacheTask implements SwitchTenantTask
Expand Down Expand Up @@ -34,5 +35,12 @@ protected function setCachePrefix(string $prefix)
config()->set('cache.prefix', $prefix);

app('cache')->forgetDriver($this->storeName);

// This is important because the `CacheManager` will have the `$app['config']` array cached
// with old prefixes on the `cache` instance. Simply calling `forgetDriver` only removes
// the `$store` but doesn't update the `$app['config']`.
app()->forgetInstance('cache');

Cache::clearResolvedInstances();
}
}
4 changes: 4 additions & 0 deletions src/Tasks/SwitchTenantDatabaseTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\Multitenancy\Tasks;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Spatie\Multitenancy\Concerns\UsesMultitenancyConfig;
use Spatie\Multitenancy\Exceptions\InvalidConfiguration;
Expand Down Expand Up @@ -44,5 +45,8 @@ protected function setTenantConnectionDatabaseName(?string $databaseName)
});

DB::purge($tenantConnectionName);

// Octane will have an old `db` instance in the Model::$resolver.
Model::setConnectionResolver(app('db'));
}
}
3 changes: 3 additions & 0 deletions tests/Feature/Tasks/SwitchTenantDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\Multitenancy\Models\Tenant;
use Spatie\Multitenancy\Tasks\SwitchTenantDatabaseTask;
use Spatie\Multitenancy\Tests\TestCase;
use Spatie\Multitenancy\Tests\TestClasses\User;

class SwitchTenantDatabaseTest extends TestCase
{
Expand Down Expand Up @@ -43,10 +44,12 @@ public function when_making_a_tenant_current_it_will_perform_the_tasks()
$this->tenant->makeCurrent();

$this->assertEquals('laravel_mt_tenant_1', DB::connection('tenant')->getDatabaseName());
$this->assertEquals('laravel_mt_tenant_1', app(User::class)->getConnection()->getDatabaseName());

$this->anotherTenant->makeCurrent();

$this->assertEquals('laravel_mt_tenant_2', DB::connection('tenant')->getDatabaseName());
$this->assertEquals('laravel_mt_tenant_2', app(User::class)->getConnection()->getDatabaseName());

Tenant::forgetCurrent();

Expand Down