Skip to content

Commit

Permalink
Add type parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Jun 21, 2019
1 parent 599b81e commit 0a0ed56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boole

$belongsTo->getQuery()->mergeConstraintsFrom($relation->getQuery());

if ($callback) {
$callback = function ($query) use ($callback, $type) {
return $callback($query, $type);
};
}

$query->where($relation->getMorphType(), '=', $type)
->whereHas($belongsTo, $callback, $operator, $count);
});
Expand Down
17 changes: 16 additions & 1 deletion tests/Integration/Database/EloquentWhereHasMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function testWhereHasMorphWithMorphMap()
}
}

public function testWhereHasMorphWithConstraint()
public function testWhereHasMorphWithRelationConstraint()
{
$comments = Comment::whereHasMorph('commentableWithConstraint', Video::class, function (Builder $query) {
$query->where('title', 'like', 'ba%');
Expand All @@ -100,6 +100,21 @@ public function testWhereHasMorphWithConstraint()
$this->assertEquals([5], $comments->pluck('id')->all());
}

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

if ($type === Video::class) {
$query->where('title', 'bar');
}
})->get();

$this->assertEquals([1, 5], $comments->pluck('id')->all());
}

public function testWhereHasMorphWithOwnerKey()
{
Schema::table('posts', function (Blueprint $table) {
Expand Down

0 comments on commit 0a0ed56

Please sign in to comment.