Skip to content

Commit

Permalink
Fix loadMissing() relationship parsing (#24329)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed May 26, 2018
1 parent 3976a43 commit 766c862
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Illuminate/Database/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use LogicException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Contracts\Queue\QueueableCollection;
Expand Down Expand Up @@ -79,7 +80,13 @@ public function loadMissing($relations)
$key = $value;
}

$path = array_combine($segments = explode('.', $key), $segments);
$segments = explode('.', explode(':', $key)[0]);

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

$path = array_combine($segments, $segments);

if (is_callable($value)) {
$path[end($segments)] = $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public function testLoadMissing()

\DB::enableQueryLog();

$posts->loadMissing('comments.parent:id.revisions', 'user:id');
$posts->loadMissing('comments.parent.revisions:revisions.comment_id', 'user:id');

$this->assertCount(2, \DB::getQueryLog());
$this->assertTrue($posts[0]->comments[0]->relationLoaded('parent'));
$this->assertTrue($posts[0]->comments[1]->parent->relationLoaded('revisions'));
$this->assertFalse(array_key_exists('post_id', $posts[0]->comments[1]->parent->getAttributes()));
$this->assertFalse(array_key_exists('id', $posts[0]->comments[1]->parent->revisions[0]->getAttributes()));
}

public function testLoadMissingWithClosure()
Expand Down

0 comments on commit 766c862

Please sign in to comment.