Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boole
$types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType())->filter()->all();
}

if (empty($types)) {
return $this->where(new Expression('0'), $operator, $count, $boolean);
}

foreach ($types as &$type) {
$type = Relation::getMorphedModel($type) ?? $type;
}
Expand Down
40 changes: 39 additions & 1 deletion tests/Integration/Database/EloquentWhereHasMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()

Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->morphs('commentable');
$table->nullableMorphs('commentable');
$table->softDeletes();
});

Expand All @@ -41,6 +41,8 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
$models[] = Video::create(['title' => 'foo']);
$models[] = Video::create(['title' => 'bar']);
$models[] = Video::create(['title' => 'baz']);
$models[] = null; // deleted
$models[] = null; // deleted

foreach ($models as $model) {
(new Comment)->commentable()->associate($model)->save();
Expand Down Expand Up @@ -103,6 +105,19 @@ public function testWhereHasMorphWithWildcardAndMorphMap()
}
}

public function testWhereHasMorphWithWildcardAndOnlyNullMorphTypes()
{
Comment::whereNotNull('commentable_type')->forceDelete();

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

$this->assertEmpty($comments->pluck('id')->all());
}

public function testWhereHasMorphWithRelationConstraint()
{
$comments = Comment::whereHasMorph('commentableWithConstraint', Video::class, function (Builder $query) {
Expand Down Expand Up @@ -190,6 +205,18 @@ public function testOrWhereHasMorph()
$this->assertEquals([1, 4], $comments->pluck('id')->all());
}

public function testOrWhereHasMorphWithWildcardAndOnlyNullMorphTypes()
{
Comment::whereNotNull('commentable_type')->forceDelete();

$comments = Comment::where('id', 7)
->orWhereHasMorph('commentable', '*', function (Builder $query) {
$query->where('title', 'foo');
})->orderBy('id')->get();

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

public function testWhereDoesntHaveMorph()
{
$comments = Comment::whereDoesntHaveMorph('commentable', Post::class, function (Builder $query) {
Expand All @@ -199,6 +226,17 @@ public function testWhereDoesntHaveMorph()
$this->assertEquals([2, 3], $comments->pluck('id')->all());
}

public function testWhereDoesntHaveMorphWithWildcardAndOnlyNullMorphTypes()
{
Comment::whereNotNull('commentable_type')->forceDelete();

$comments = Comment::whereDoesntHaveMorph('commentable', [], function (Builder $query) {
$query->where('title', 'foo');
})->orderBy('id')->get();

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

public function testOrWhereDoesntHaveMorph()
{
$comments = Comment::where('id', 1)
Expand Down