Skip to content

Commit

Permalink
Docblock cleanup and support single relations
Browse files Browse the repository at this point in the history
  • Loading branch information
brendt committed May 31, 2019
1 parent 5f1dc4e commit a6700e5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion tests/Integration/Database/EloquentMorphEagerLoadingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,26 @@ 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();

$this->assertTrue($comments[0]->relationLoaded('commentable'));
$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
Expand Down

0 comments on commit a6700e5

Please sign in to comment.