diff --git a/src/Database/Queries/AbstractEloquentQuery.php b/src/Database/Queries/AbstractEloquentQuery.php index 2c9cad77..4b82200a 100644 --- a/src/Database/Queries/AbstractEloquentQuery.php +++ b/src/Database/Queries/AbstractEloquentQuery.php @@ -5,7 +5,6 @@ namespace LaraStrict\Database\Queries; use Closure; -use Exception; use Illuminate\Contracts\Pagination\LengthAwarePaginator; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; @@ -14,6 +13,7 @@ use Illuminate\Database\Eloquent\Scope; use Illuminate\Support\Arr; use LaraStrict\Database\Scopes\OrderScope; +use Throwable; /** * You must implement abstract public function execute. @@ -45,7 +45,7 @@ public function make(array $attributes = []): Model abstract protected function getModelClass(): string; /** - * @param array $scopes + * @param array $scopes * @param array $orderBy * * @return ChunkedModelQueryResult @@ -150,13 +150,13 @@ protected function find(string|int $id, array $scopes = []): ?Model } /** - * @param array $scopes - * @param Closure(int|string):Exception|null $customException Creates a custom exceptions if model does not exists. - * Receives $id argument. - * + * @template TException of Throwable + * @param array $scopes + * @param Closure(int|string):TException|TException|null $customException Creates a custom exceptions if model does + * not exists. Receives $id argument. * @return TModel */ - protected function findOrFail(string|int $key, array $scopes = [], Closure $customException = null): Model + protected function findOrFail(string|int $key, array $scopes = [], Closure|Throwable $customException = null): Model { try { /** @var TModel $model */ @@ -167,8 +167,9 @@ protected function findOrFail(string|int $key, array $scopes = [], Closure $cust } catch (ModelNotFoundException $modelNotFoundException) { if ($customException === null) { throw $modelNotFoundException; + } elseif ($customException instanceof Throwable) { + throw $customException; } - throw $customException($key); } } @@ -213,6 +214,7 @@ protected function getQuery(array $scopes = []): Builder $class = $this->getModelClass(); /** @var Builder $query */ $query = $class::query(); + return $this->getScopedQuery($query, $scopes); } }