Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TModel> : 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.
*
Expand Down Expand Up @@ -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-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TModel> : 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.
*
Expand Down
51 changes: 51 additions & 0 deletions Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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-key, mixed>|array<mixed>) ? 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-key, mixed>|array<mixed>) ? 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.
*
Expand Down
Loading