Skip to content

[12.x] Add support for nested array notation within loadMissing #54554

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: 12.x
Choose a base branch
from
Draft
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
36 changes: 35 additions & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ public function loadMissing($relations)
$relations = func_get_args();
}

foreach ($relations as $key => $value) {
foreach ($this->prepareLoadMissingRelationships($relations) as $key => $value) {
if (is_numeric($key)) {
$key = $value;
}
Expand All @@ -248,6 +248,40 @@ public function loadMissing($relations)
return $this;
}

/**
* Prepare nested load missing relationships.
*
* @param array $relations
* @param string $prefix
* @return array
*/
protected function prepareLoadMissingRelationships($relations, $prefix = '')
{
$preparedRelationships = [];

foreach ($relations as $key => $value) {
$fullKey = $prefix ? "$prefix.$key" : $key;

// If the value is not an array, it can be used as-is
if (! is_array($value)) {
$preparedRelationships[$fullKey] = $value;
} else {
if (array_values($value) === $value) {
// If the array has a depth of 1, we simply flatten the array
foreach ($value as $subValue) {
$preparedRelationships["$fullKey.$subValue"] = null;
}
} else {
// We now know that the remaining relationships are nested arrays
// so we must prepare them recursively
$preparedRelationships = array_merge($preparedRelationships, $this->prepareLoadMissingRelationships($value, $fullKey));
}
}
}

return $preparedRelationships;
}

/**
* Load a relationship path if it is not already eager loaded.
*
Expand Down
71 changes: 71 additions & 0 deletions tests/Integration/Database/EloquentCollectionLoadMissingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,77 @@ public function testLoadMissingWithClosure()
$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]);
}

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