Skip to content
Merged
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
33 changes: 17 additions & 16 deletions src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,28 +222,29 @@ public function loadMissing($relations)
$relations = func_get_args();
}

foreach ($relations as $key => $value) {
if (is_numeric($key)) {
$key = $value;
}
if ($this->isNotEmpty()) {
$query = $this->first()->newQueryWithoutRelationships()->with($relations);

$segments = explode('.', explode(':', $key)[0]);
foreach ($query->getEagerLoads() as $key => $value) {
$segments = explode('.', explode(':', $key)[0]);

if (str_contains($key, ':')) {
$segments[count($segments) - 1] .= ':'.explode(':', $key)[1];
}
if (str_contains($key, ':')) {
$segments[count($segments) - 1] .= ':'.explode(':', $key)[1];
}

$path = [];
$path = [];

foreach ($segments as $segment) {
$path[] = [$segment => $segment];
}
foreach ($segments as $segment) {
$path[] = [$segment => $segment];
}

if (is_callable($value)) {
$path[count($segments) - 1][array_last($segments)] = $value;
}
if (is_callable($value)) {
$path[count($segments) - 1][array_last($segments)] = $value;
}

$this->loadMissingRelation($this, $path);

$this->loadMissingRelation($this, $path);
}
}

return $this;
Expand Down
118 changes: 118 additions & 0 deletions tests/Integration/Database/EloquentCollectionLoadMissingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,123 @@ public function testLoadMissingWithoutInitialLoad()
$this->assertEquals(1, $user->posts[1]->postRelation->postSubRelations[0]->postSubSubRelations->count());
$this->assertInstanceOf(PostSubSubRelation::class, $user->posts[1]->postRelation->postSubRelations[0]->postSubSubRelations[0]);
}

public function testLoadMissingWithNestedArraySyntax()
{
$posts = Post::with('user')->get();

DB::enableQueryLog();

$posts->loadMissing([
'comments' => ['parent'],
'user',
]);

$this->assertCount(2, DB::getQueryLog());
$this->assertTrue($posts[0]->comments[0]->relationLoaded('parent'));
$this->assertTrue($posts[0]->relationLoaded('user'));
}

public function testLoadMissingWithMultipleDotNotationRelations()
{
$posts = Post::with('comments')->get();

DB::enableQueryLog();

$posts->loadMissing([
'comments.parent',
'user.posts',
]);

$this->assertCount(3, DB::getQueryLog());
$this->assertTrue($posts[0]->comments[0]->relationLoaded('parent'));
$this->assertTrue($posts[0]->relationLoaded('user'));
$this->assertTrue($posts[0]->user->relationLoaded('posts'));
}

public function testLoadMissingWithNestedArrayWithColon()
{
$posts = Post::with('comments')->get();

DB::enableQueryLog();

$posts->loadMissing(['comments' => ['parent:id']]);

$this->assertCount(1, DB::getQueryLog());
$this->assertTrue($posts[0]->comments[0]->relationLoaded('parent'));
$this->assertArrayNotHasKey('post_id', $posts[0]->comments[1]->parent->getAttributes());
}

public function testLoadMissingWithNestedArray()
{
$posts = Post::with('comments')->get();

DB::enableQueryLog();

$posts->loadMissing(['comments' => ['parent']]);

$this->assertCount(1, DB::getQueryLog());
$this->assertTrue($posts[0]->comments[0]->relationLoaded('parent'));
}

public function testLoadMissingWithNestedArrayWithClosure()
{
$posts = Post::with('comments')->get();

DB::enableQueryLog();

$posts->loadMissing(['comments' => ['parent' => function ($query) {
$query->select('id');
}]]);

$this->assertCount(1, DB::getQueryLog());
$this->assertTrue($posts[0]->comments[0]->relationLoaded('parent'));
$this->assertArrayNotHasKey('post_id', $posts[0]->comments[1]->parent->getAttributes());
}

public function testLoadMissingWithMultipleNestedArrays()
{
$users = User::get();
$users->loadMissing([
'posts' => [
'postRelation' => [
'postSubRelations' => [
'postSubSubRelations',
],
],
],
]);

$user = $users->first();
$this->assertEquals(2, $user->posts->count());
$this->assertNull($user->posts[0]->postRelation);
$this->assertInstanceOf(PostRelation::class, $user->posts[1]->postRelation);
$this->assertEquals(1, $user->posts[1]->postRelation->postSubRelations->count());
$this->assertInstanceOf(PostSubRelation::class, $user->posts[1]->postRelation->postSubRelations[0]);
$this->assertEquals(1, $user->posts[1]->postRelation->postSubRelations[0]->postSubSubRelations->count());
$this->assertInstanceOf(PostSubSubRelation::class, $user->posts[1]->postRelation->postSubRelations[0]->postSubSubRelations[0]);
}

public function testLoadMissingWithMultipleNestedArraysCombinedWithDotNotation()
{
$users = User::get();
$users->loadMissing([
'posts' => [
'postRelation' => [
'postSubRelations.postSubSubRelations',
],
],
]);

$user = $users->first();
$this->assertEquals(2, $user->posts->count());
$this->assertNull($user->posts[0]->postRelation);
$this->assertInstanceOf(PostRelation::class, $user->posts[1]->postRelation);
$this->assertEquals(1, $user->posts[1]->postRelation->postSubRelations->count());
$this->assertInstanceOf(PostSubRelation::class, $user->posts[1]->postRelation->postSubRelations[0]);
$this->assertEquals(1, $user->posts[1]->postRelation->postSubRelations[0]->postSubSubRelations->count());
$this->assertInstanceOf(PostSubSubRelation::class, $user->posts[1]->postRelation->postSubRelations[0]->postSubSubRelations[0]);
}
}

class Comment extends Model
Expand Down Expand Up @@ -200,6 +317,7 @@ class Revision extends Model
class User extends Model
{
public $timestamps = false;
protected $guarded = [];

public function posts()
{
Expand Down