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
59 changes: 24 additions & 35 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ trait QueriesRelationships
* @param string $operator
* @param int $count
* @param string $boolean
* @param \Closure|null $callback
* @return $this
*
* @throws \RuntimeException
Expand Down Expand Up @@ -123,7 +122,6 @@ public function orHas($relation, $operator = '>=', $count = 1)
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param string $boolean
* @param \Closure|null $callback
* @return $this
*/
public function doesntHave($relation, $boolean = 'and', ?Closure $callback = null)
Expand All @@ -146,7 +144,6 @@ public function orDoesntHave($relation)
* Add a relationship count / exists condition to the query with where clauses.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Closure|null $callback
* @param string $operator
* @param int $count
* @return $this
Expand All @@ -162,7 +159,6 @@ public function whereHas($relation, ?Closure $callback = null, $operator = '>=',
* Also load the relationship with same condition.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @param string $operator
* @param int $count
* @return $this
Expand All @@ -177,7 +173,6 @@ public function withWhereHas($relation, ?Closure $callback = null, $operator = '
* Add a relationship count / exists condition to the query with where clauses and an "or".
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @param string $operator
* @param int $count
* @return $this
Expand All @@ -191,7 +186,6 @@ public function orWhereHas($relation, ?Closure $callback = null, $operator = '>=
* Add a relationship count / exists condition to the query with where clauses.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @return $this
*/
public function whereDoesntHave($relation, ?Closure $callback = null)
Expand All @@ -203,7 +197,6 @@ public function whereDoesntHave($relation, ?Closure $callback = null)
* Add a relationship count / exists condition to the query with where clauses and an "or".
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @return $this
*/
public function orWhereDoesntHave($relation, ?Closure $callback = null)
Expand All @@ -219,7 +212,6 @@ public function orWhereDoesntHave($relation, ?Closure $callback = null)
* @param string $operator
* @param int $count
* @param string $boolean
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @return $this
*/
public function hasMorph($relation, $types, $operator = '>=', $count = 1, $boolean = 'and', ?Closure $callback = null)
Expand All @@ -230,6 +222,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 +243,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 Expand Up @@ -308,7 +308,6 @@ public function orHasMorph($relation, $types, $operator = '>=', $count = 1)
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
* @param string|array $types
* @param string $boolean
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @return $this
*/
public function doesntHaveMorph($relation, $types, $boolean = 'and', ?Closure $callback = null)
Expand All @@ -333,7 +332,6 @@ public function orDoesntHaveMorph($relation, $types)
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
* @param string|array $types
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @param string $operator
* @param int $count
* @return $this
Expand All @@ -348,7 +346,6 @@ public function whereHasMorph($relation, $types, ?Closure $callback = null, $ope
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
* @param string|array $types
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @param string $operator
* @param int $count
* @return $this
Expand All @@ -363,7 +360,6 @@ public function orWhereHasMorph($relation, $types, ?Closure $callback = null, $o
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
* @param string|array $types
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @return $this
*/
public function whereDoesntHaveMorph($relation, $types, ?Closure $callback = null)
Expand All @@ -376,7 +372,6 @@ public function whereDoesntHaveMorph($relation, $types, ?Closure $callback = nul
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<*, *>|string $relation
* @param string|array $types
* @param \Closure|null $callback
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @return $this
*/
public function orWhereDoesntHaveMorph($relation, $types, ?Closure $callback = null)
Expand Down Expand Up @@ -930,11 +925,6 @@ public function mergeConstraintsFrom(Builder $from)

/**
* Updates the table name for any columns with a new qualified name.
*
* @param array $wheres
* @param string $from
* @param string $to
* @return array
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
*/
protected function requalifyWhereTables(array $wheres, string $from, string $to): array
{
Expand All @@ -950,7 +940,6 @@ protected function requalifyWhereTables(array $wheres, string $from, string $to)
/**
* Add a sub-query count clause to this query.
*
* @param \Illuminate\Database\Query\Builder $query
liamduckett marked this conversation as resolved.
Show resolved Hide resolved
* @param string $operator
* @param int $count
* @param string $boolean
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
Loading