From 40e4f4f2d27914bcde2820373d4aca29c1c9b05a Mon Sep 17 00:00:00 2001 From: Marc Edwards Date: Tue, 26 Aug 2025 22:00:58 -0400 Subject: [PATCH 1/2] Adds findWith and findOrFailWith methods to lluminate\Database\Eloquent\Builder and lluminate\Database\Eloquent\Collection --- Eloquent/Builder.php | 50 ++++++++++++++++++++++++++++++++++++++ Eloquent/Collection.php | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 103 insertions(+) diff --git a/Eloquent/Builder.php b/Eloquent/Builder.php index 1427c016f..ce592b0a9 100755 --- a/Eloquent/Builder.php +++ b/Eloquent/Builder.php @@ -623,6 +623,31 @@ public function findOrFail($id, $columns = ['*']) return $result; } + /** + * Find a model by its primary key with eager loaded relations, or throw an exception. + * + * @param mixed $id + * @param array|string $relations + * @param array|string $columns + * @return ($id is (\Illuminate\Contracts\Support\Arrayable|array) ? \Illuminate\Database\Eloquent\Collection : TModel|null) + */ + public function findOrFailWith($id, $relations, $columns = ['*']) + { + if (is_string($relations)) { + $relations = [$relations]; + } + + $result = $this->findOrFail($id, $columns); + if ($result instanceof Collection) { + $result->loadMissing($relations); + } + if ($result instanceof Model) { + $result->loadMissing($relations); + } + + return $result; + } + /** * Find a model by its primary key or return fresh model instance. * @@ -668,6 +693,31 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null) return $callback(); } + /** + * Find a model by its primary key with eager loaded relations. + * + * @param mixed $id + * @param array|string $relations + * @param array|string $columns + * @return ($id is (\Illuminate\Contracts\Support\Arrayable|array) ? \Illuminate\Database\Eloquent\Collection : TModel|null) + */ + public function findWith($id, $relations, $columns = ['*']) + { + if (is_string($relations)) { + $relations = [$relations]; + } + + $result = $this->find($id, $columns); + if ($result instanceof Collection) { + $result->loadMissing($relations); + } + if ($result instanceof Model) { + $result->loadMissing($relations); + } + + return $result; + } + /** * Get the first record matching the attributes or instantiate it. * diff --git a/Eloquent/Collection.php b/Eloquent/Collection.php index 3f2679149..4210ef201 100755 --- a/Eloquent/Collection.php +++ b/Eloquent/Collection.php @@ -82,6 +82,59 @@ public function findOrFail($key) throw $exception; } + /** + * Find a model in the collection by key with eager loaded relations. + * + * @template TFindDefault + * + * @param mixed $key + * @param string|array $relations; + * @return ($key is (\Illuminate\Contracts\Support\Arrayable|array) ? static : TModel) + */ + public function findOrFailWith($key, $relations) + { + if (is_string($relations)) { + $relations = [$relations]; + } + + $result = $this->findOrFail($key); + if ($result instanceof Collection) { + $result->loadMissing($relations); + } + if ($result instanceof Model) { + $result->loadMissing($relations); + } + + return $result; + } + + /** + * Find a model in the collection by key with eager loaded relations. + * + * @template TFindDefault + * + * @param mixed $key + * @param string|array $relations; + * @param TFindDefault $default + * @return ($key is (\Illuminate\Contracts\Support\Arrayable|array) ? static : TModel|TFindDefault) + */ + public function findWith($key, $relations, $default = null) + { + if (is_string($relations)) { + $relations = [$relations]; + } + + $result = $this->find($key, $default); + if ($result instanceof Collection) { + $result->loadMissing($relations); + } + if ($result instanceof Model) { + $result->loadMissing($relations); + } + + return $result; + } + /** * Load a set of relationships onto the collection. * From dffa58d5307fbf6f65ee2d8ad7080be2ff9072e4 Mon Sep 17 00:00:00 2001 From: Marc Edwards Date: Wed, 27 Aug 2025 08:37:46 -0400 Subject: [PATCH 2/2] Cleanup --- Eloquent/Collection.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/Eloquent/Collection.php b/Eloquent/Collection.php index 4210ef201..13f0ed5e9 100755 --- a/Eloquent/Collection.php +++ b/Eloquent/Collection.php @@ -85,8 +85,6 @@ public function findOrFail($key) /** * Find a model in the collection by key with eager loaded relations. * - * @template TFindDefault - * * @param mixed $key * @param string|array $relations; * @return ($key is (\Illuminate\Contracts\Support\Arrayable|array) ? static : TModel)