From 0637df2aa8181fdee4ffab8cbeb3cd8992977580 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sat, 26 May 2018 00:59:59 +0200 Subject: [PATCH] Fix loadMissing() relationship parsing --- src/Illuminate/Database/Eloquent/Collection.php | 9 ++++++++- .../Database/EloquentCollectionLoadMissingTest.php | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Collection.php b/src/Illuminate/Database/Eloquent/Collection.php index 4114d8afe3d6..ad2a7a115c57 100755 --- a/src/Illuminate/Database/Eloquent/Collection.php +++ b/src/Illuminate/Database/Eloquent/Collection.php @@ -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; @@ -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; diff --git a/tests/Integration/Database/EloquentCollectionLoadMissingTest.php b/tests/Integration/Database/EloquentCollectionLoadMissingTest.php index 93942538b0a4..b04d832bd01b 100644 --- a/tests/Integration/Database/EloquentCollectionLoadMissingTest.php +++ b/tests/Integration/Database/EloquentCollectionLoadMissingTest.php @@ -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()