Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Where doesnt have nullable morph #54363

Open
wants to merge 10 commits into
base: 11.x
Choose a base branch
from
40 changes: 24 additions & 16 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,12 @@ public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boole

$types = (array) $types;

$checkMorphNull = $types === ['*']
&& (($operator === '<' && $count >= 1)
|| ($operator === '<=' && $count >= 0)
|| ($operator === '=' && $count === 0)
|| ($operator === '!=' && $count >= 1));

if ($types === ['*']) {
$types = $this->model->newModelQuery()->distinct()->pluck($relation->getMorphType())
->filter()
Expand All @@ -245,22 +251,24 @@ public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boole
$type = Relation::getMorphedModel($type) ?? $type;
}

return $this->where(function ($query) use ($relation, $callback, $operator, $count, $types) {
foreach ($types as $type) {
$query->orWhere(function ($query) use ($relation, $callback, $operator, $count, $type) {
$belongsTo = $this->getBelongsToRelation($relation, $type);

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

$query->where($this->qualifyColumn($relation->getMorphType()), '=', (new $type)->getMorphClass())
->whereHas($belongsTo, $callback, $operator, $count);
});
}
}, null, null, $boolean);
return $this
->where(function ($query) use ($relation, $callback, $operator, $count, $types) {
foreach ($types as $type) {
$query->orWhere(function ($query) use ($relation, $callback, $operator, $count, $type) {
$belongsTo = $this->getBelongsToRelation($relation, $type);

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

$query->where($this->qualifyColumn($relation->getMorphType()), '=', (new $type)->getMorphClass())
->whereHas($belongsTo, $callback, $operator, $count);
});
}
}, null, null, $boolean)
->when($checkMorphNull, fn (self $query) => $query->orWhereMorphedTo($relation, null));
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/Database/EloquentWhereHasMorphTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,13 @@ public function testModelScopesAreAccessible()

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

public function testWhereDoesntHaveMorphWithNullableMorph()
{
$comments = Comment::whereDoesntHaveMorph('commentable', '*')->orderBy('id')->get();

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

class Comment extends Model
Expand Down