From 0b97e6e2fa075c1774327ff59183d3d853d47e48 Mon Sep 17 00:00:00 2001 From: Mustapha Tijani Date: Sun, 24 Mar 2024 23:28:39 +0100 Subject: [PATCH] Added ability for custom resolving column in PathTenantResolver --- .../TenantCouldNotBeIdentifiedByPathException.php | 4 ++-- src/Resolvers/PathTenantResolver.php | 11 ++++++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Exceptions/TenantCouldNotBeIdentifiedByPathException.php b/src/Exceptions/TenantCouldNotBeIdentifiedByPathException.php index 896c9323..68db2280 100644 --- a/src/Exceptions/TenantCouldNotBeIdentifiedByPathException.php +++ b/src/Exceptions/TenantCouldNotBeIdentifiedByPathException.php @@ -11,9 +11,9 @@ use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException; 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 diff --git a/src/Resolvers/PathTenantResolver.php b/src/Resolvers/PathTenantResolver.php index e3c32cc7..bcec9474 100644 --- a/src/Resolvers/PathTenantResolver.php +++ b/src/Resolvers/PathTenantResolver.php @@ -21,20 +21,25 @@ class PathTenantResolver extends Contracts\CachedTenantResolver /** @var string|null */ public static $cacheStore = null; // default + /** @var string */ + public static $resolvingColumn = 'id'; // default + public function resolveWithoutCache(...$args): Tenant { /** @var Route $route */ $route = $args[0]; - if ($id = $route->parameter(static::$tenantParameterName)) { + $resolvingColumn = static::$resolvingColumn; + + if ($value = $route->parameter(static::$tenantParameterName)) { $route->forgetParameter(static::$tenantParameterName); - if ($tenant = tenancy()->find($id)) { + if ($tenant = tenancy()->where($resolvingColumn, $value)->first()) { return $tenant; } } - throw new TenantCouldNotBeIdentifiedByPathException($id); + throw new TenantCouldNotBeIdentifiedByPathException($resolvingColumn, $value); } public function resolved(Tenant $tenant, ...$args): void