diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 1ea2ccc3cfb3..28867c7e4d90 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -120,7 +120,7 @@ protected function getResultsByType($type) ->mergeConstraintsFrom($this->getQuery()) ->with(array_merge( $this->getQuery()->getEagerLoads(), - $this->typedEagerLoads[get_class($instance)] ?? [] + (array) ($this->typedEagerLoads[get_class($instance)] ?? []) )); return $query->whereIn( @@ -266,9 +266,7 @@ public function getDictionary() /** * Specify which relations to load for a given morph type. * - * @param string $modelClass * @param array $with - * * @return \Illuminate\Database\Eloquent\Relations\MorphTo */ public function withMorph(array $with) diff --git a/tests/Integration/Database/EloquentMorphEagerLoadingTest.php b/tests/Integration/Database/EloquentMorphEagerLoadingTest.php index 2e63143b88b0..b7ba197a6f75 100644 --- a/tests/Integration/Database/EloquentMorphEagerLoadingTest.php +++ b/tests/Integration/Database/EloquentMorphEagerLoadingTest.php @@ -50,7 +50,7 @@ public function test_with_morph_loading() { $comments = Comment::query() ->with(['commentable' => function (MorphTo $morphTo) { - $morphTo->withMorph([Post::class =>['user']]); + $morphTo->withMorph([Post::class => ['user']]); }]) ->get(); @@ -58,6 +58,18 @@ public function test_with_morph_loading() $this->assertTrue($comments[0]->commentable->relationLoaded('user')); $this->assertTrue($comments[1]->relationLoaded('commentable')); } + + public function test_with_morph_loading_with_single_relation() + { + $comments = Comment::query() + ->with(['commentable' => function (MorphTo $morphTo) { + $morphTo->withMorph([Post::class => 'user']); + }]) + ->get(); + + $this->assertTrue($comments[0]->relationLoaded('commentable')); + $this->assertTrue($comments[0]->commentable->relationLoaded('user')); + } } class Comment extends Model