1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 01:54:04 +00:00

Added ability for custom resolving column in PathTenantResolver

This commit is contained in:
Mustapha Tijani 2024-03-24 23:28:39 +01:00
parent 72b1b48edd
commit 0b97e6e2fa
2 changed files with 10 additions and 5 deletions

View file

@ -11,9 +11,9 @@ use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException;
class TenantCouldNotBeIdentifiedByPathException extends TenantCouldNotBeIdentifiedException implements ProvidesSolution class TenantCouldNotBeIdentifiedByPathException extends TenantCouldNotBeIdentifiedException implements ProvidesSolution
{ {
public function __construct($tenant_id) public function __construct($resolvingColumn, $value)
{ {
parent::__construct("Tenant could not be identified on path with tenant_id: $tenant_id"); parent::__construct("Tenant could not be identified on path with tenant_$resolvingColumn: $value");
} }
public function getSolution(): Solution public function getSolution(): Solution

View file

@ -21,20 +21,25 @@ class PathTenantResolver extends Contracts\CachedTenantResolver
/** @var string|null */ /** @var string|null */
public static $cacheStore = null; // default public static $cacheStore = null; // default
/** @var string */
public static $resolvingColumn = 'id'; // default
public function resolveWithoutCache(...$args): Tenant public function resolveWithoutCache(...$args): Tenant
{ {
/** @var Route $route */ /** @var Route $route */
$route = $args[0]; $route = $args[0];
if ($id = $route->parameter(static::$tenantParameterName)) { $resolvingColumn = static::$resolvingColumn;
if ($value = $route->parameter(static::$tenantParameterName)) {
$route->forgetParameter(static::$tenantParameterName); $route->forgetParameter(static::$tenantParameterName);
if ($tenant = tenancy()->find($id)) { if ($tenant = tenancy()->where($resolvingColumn, $value)->first()) {
return $tenant; return $tenant;
} }
} }
throw new TenantCouldNotBeIdentifiedByPathException($id); throw new TenantCouldNotBeIdentifiedByPathException($resolvingColumn, $value);
} }
public function resolved(Tenant $tenant, ...$args): void public function resolved(Tenant $tenant, ...$args): void