Skip to content

Commit

Permalink
Support wildcard and morph map
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Jun 25, 2019
1 parent 0b88202 commit c676fc0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,11 @@ public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boole
$types = (array) $types;

if ($types === ['*']) {
$types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType());
$types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType())->all();

foreach ($types as &$type) {
$type = Relation::getMorphedModel($type) ?? $type;
}
}

return $this->where(function ($query) use ($relation, $callback, $operator, $count, $types) {
Expand Down
34 changes: 34 additions & 0 deletions tests/Integration/Database/EloquentWhereHasMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function testWhereHasMorph()
$this->assertEquals([1, 4], $comments->pluck('id')->all());
}

public function testWhereHasMorphWithMorphMap()
{
Relation::morphMap(['posts' => Post::class]);

Comment::where('commentable_type', Post::class)->update(['commentable_type' => 'posts']);

try {
$comments = Comment::whereHasMorph('commentable', [Post::class, Video::class], function (Builder $query) {
$query->where('title', 'foo');
})->get();

$this->assertEquals([1, 4], $comments->pluck('id')->all());
} finally {
Relation::morphMap([], false);
}
}

public function testWhereHasMorphWithWildcard()
{
// Test newModelQuery() without global scopes.
Expand All @@ -74,6 +91,23 @@ public function testWhereHasMorphWithWildcard()
$this->assertEquals([1, 4], $comments->pluck('id')->all());
}

public function testWhereHasMorphWithWildcardAndMorphMap()
{
Relation::morphMap(['posts' => Post::class]);

Comment::where('commentable_type', Post::class)->update(['commentable_type' => 'posts']);

try {
$comments = Comment::whereHasMorph('commentable', '*', function (Builder $query) {
$query->where('title', 'foo');
})->get();

$this->assertEquals([4, 1], $comments->pluck('id')->all());
} finally {
Relation::morphMap([], false);
}
}

public function testWhereHasMorphWithRelationConstraint()
{
$comments = Comment::whereHasMorph('commentableWithConstraint', Video::class, function (Builder $query) {
Expand Down

0 comments on commit c676fc0

Please sign in to comment.