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..13f0ed5e9 100755 --- a/Eloquent/Collection.php +++ b/Eloquent/Collection.php @@ -82,6 +82,57 @@ public function findOrFail($key) throw $exception; } + /** + * Find a model in the collection by key with eager loaded relations. + * + * @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. *