Skip to content

Commit

Permalink
Allowed closures for the relation parameter inside of QueriesRelation…
Browse files Browse the repository at this point in the history
…ship.
  • Loading branch information
irealworlds committed Jul 10, 2024
1 parent 68e88bb commit 1c45d02
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
use Illuminate\Support\Str;
use InvalidArgumentException;

/** @mixin \Illuminate\Database\Eloquent\Builder */
/**
* @template TModel of \Illuminate\Database\Eloquent\Model
* @mixin \Illuminate\Database\Eloquent\Builder<TModel>
*/
trait QueriesRelationships
{
/**
* Add a relationship count / exists condition to the query.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relations|string $relation
* @param string $operator
* @param int $count
* @param string $boolean
Expand All @@ -38,6 +41,10 @@ public function has($relation, $operator = '>=', $count = 1, $boolean = 'and', ?
}

$relation = $this->getRelationWithoutConstraints($relation);
} else if ($relation instanceof Closure) {
$relation = Relation::noConstraints(function () use ($relation) {
return $relation($this->getModel());
});
}

if ($relation instanceof MorphTo) {
Expand Down Expand Up @@ -105,7 +112,7 @@ protected function hasNested($relations, $operator = '>=', $count = 1, $boolean
/**
* Add a relationship count / exists condition to the query with an "or".
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relation|string $relation
* @param string $operator
* @param int $count
* @return $this
Expand All @@ -118,7 +125,7 @@ public function orHas($relation, $operator = '>=', $count = 1)
/**
* Add a relationship count / exists condition to the query.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relation|string $relation
* @param string $boolean
* @param \Closure|null $callback
* @return $this
Expand All @@ -131,7 +138,7 @@ public function doesntHave($relation, $boolean = 'and', ?Closure $callback = nul
/**
* Add a relationship count / exists condition to the query with an "or".
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relation|string $relation
* @return $this
*/
public function orDoesntHave($relation)
Expand All @@ -142,7 +149,7 @@ 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 \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relation|string $relation
* @param \Closure|null $callback
* @param string $operator
* @param int $count
Expand Down Expand Up @@ -173,7 +180,7 @@ 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 \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relation|string $relation
* @param \Closure|null $callback
* @param string $operator
* @param int $count
Expand All @@ -187,7 +194,7 @@ 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 \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relation|string $relation
* @param \Closure|null $callback
* @return $this
*/
Expand All @@ -199,7 +206,7 @@ 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 \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|\Closure(TModel): \Illuminate\Database\Eloquent\Relation|string $relation
* @param \Closure|null $callback
* @return $this
*/
Expand Down

0 comments on commit 1c45d02

Please sign in to comment.