From ed684353ac263766af2a6b3ad98138bb27aa578a Mon Sep 17 00:00:00 2001 From: Ben Rutland Date: Thu, 21 Oct 2021 23:08:25 +0100 Subject: [PATCH 1/3] added firstOrFail method --- src/Cache/resources/config/cache.php | 4 ++-- src/Database/Builder.php | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Cache/resources/config/cache.php b/src/Cache/resources/config/cache.php index baf22b1..c0232c3 100644 --- a/src/Cache/resources/config/cache.php +++ b/src/Cache/resources/config/cache.php @@ -7,8 +7,8 @@ | Cache Key Prefix |-------------------------------------------------------------------------- | - | We'll specify a value to get prefixed to all our keys so we can avoid - | collisions with plugins that migt be setting the same keys. + | We'll specify a value to get prefixed to all our keys so we can avoid + | collisions with plugins that might be setting the same keys. | */ diff --git a/src/Database/Builder.php b/src/Database/Builder.php index 819283f..032ecdf 100644 --- a/src/Database/Builder.php +++ b/src/Database/Builder.php @@ -331,6 +331,23 @@ public function first() return $this->take(1)->get()->first(); } + /** + * Get the first record or throw an exception + * + * @param string $column + * @return \Radiate\Database\Model|mixed + * + * @throws \Radiate\Database\ModelNotFoundException + */ + public function firstOrFail() + { + if ($first = $this->first()) { + return $first; + } + + throw new ModelNotFoundException(get_class($this->model)); + } + /** * Dynamically handle calls into the query instance. * From d8fb7c4a499948fadb0c6a24ca2ca2106172d7d4 Mon Sep 17 00:00:00 2001 From: Ben Rutland Date: Thu, 21 Oct 2021 23:14:17 +0100 Subject: [PATCH 2/3] changed model not found exception --- src/Database/ModelNotFoundException.php | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/Database/ModelNotFoundException.php b/src/Database/ModelNotFoundException.php index 94fc8e8..1635257 100644 --- a/src/Database/ModelNotFoundException.php +++ b/src/Database/ModelNotFoundException.php @@ -2,10 +2,9 @@ namespace Radiate\Database; -use Radiate\Foundation\Http\Exceptions\HttpResponseException; -use Radiate\Http\Response; +use RuntimeException; -class ModelNotFoundException extends HttpResponseException +class ModelNotFoundException extends RuntimeException { /** * Create a new model not found exception. @@ -15,8 +14,6 @@ class ModelNotFoundException extends HttpResponseException */ public function __construct(string $model) { - parent::__construct( - new Response("No query results for model [{$model}]", 404) - ); + parent::__construct("No query results for model [{$model}]", 404); } } From 1ad00076685b7ed9957ac835be7f57dd2488ebb6 Mon Sep 17 00:00:00 2001 From: Ben Rutland Date: Sat, 23 Oct 2021 14:15:39 +0100 Subject: [PATCH 3/3] added findOrFail --- src/Database/Builder.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Database/Builder.php b/src/Database/Builder.php index 032ecdf..5fba6b1 100644 --- a/src/Database/Builder.php +++ b/src/Database/Builder.php @@ -323,7 +323,6 @@ public function only($columns) /** * Get the first record * - * @param string $column * @return \Radiate\Database\Model|mixed */ public function first() @@ -334,7 +333,6 @@ public function first() /** * Get the first record or throw an exception * - * @param string $column * @return \Radiate\Database\Model|mixed * * @throws \Radiate\Database\ModelNotFoundException @@ -348,6 +346,23 @@ public function firstOrFail() throw new ModelNotFoundException(get_class($this->model)); } + /** + * Get the first record or throw an exception + * + * @param int $id + * @return \Radiate\Database\Model|mixed + * + * @throws \Radiate\Database\ModelNotFoundException + */ + public function findOrFail(int $id) + { + if ($found = $this->find($id)) { + return $found; + } + + throw new ModelNotFoundException(get_class($this->model)); + } + /** * Dynamically handle calls into the query instance. *