1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 04:04:04 +00:00

add Tenancy::$findWith

This commit is contained in:
Samuel Štancl 2024-04-05 19:48:50 +02:00
parent 5ef7604e17
commit ac5948dfd1
5 changed files with 24 additions and 13 deletions

View file

@ -28,6 +28,11 @@ class Tenancy
/** Is tenancy fully initialized? */
public bool $initialized = false; // todo@docs document the difference between $tenant being set and $initialized being true (e.g. end of initialize() method)
/**
* List of relations to eager load when fetching a tenant via tenancy()->find().
*/
public static array $findWith = [];
/** Initialize tenancy for the passed tenant. */
public function initialize(Tenant|int|string $tenant): void
{
@ -136,10 +141,10 @@ class Tenancy
/**
* Try to find a tenant using an ID.
*/
public static function find(int|string $id, string $column = null): (Tenant&Model)|null
public static function find(int|string $id, string $column = null, bool $withRelations = false): (Tenant&Model)|null
{
/** @var (Tenant&Model)|null $tenant */
$tenant = static::model()->firstWhere($column ?? static::model()->getTenantKeyName(), $id);
$tenant = static::model()->with($withRelations ? static::$findWith : [])->firstWhere($column ?? static::model()->getTenantKeyName(), $id);
return $tenant;
}
@ -221,6 +226,16 @@ class Tenancy
return array_keys($cachedResolvers);
}
public static function invalidateResolverCache(Tenant&Model $tenant): void
{
foreach (static::cachedResolvers() as $resolver) {
/** @var Resolvers\Contracts\CachedTenantResolver $resolver */
$resolver = app($resolver);
$resolver->invalidateCache($tenant);
}
}
/**
* Tenant identification middleware used by the package.
*