Illuminate\Database\Eloquent\Builder::hasNested PHP Method

hasNested() protected method

Add nested relationship count / exists conditions to the query.
protected hasNested ( string $relations, string $operator = '>=', integer $count = 1, string $boolean = 'and', Closure | null $callback = null ) : Builder | static
$relations string
$operator string
$count integer
$boolean string
$callback Closure | null
return Builder | static
    protected function hasNested($relations, $operator = '>=', $count = 1, $boolean = 'and', $callback = null)
    {
        $relations = explode('.', $relations);
        // In order to nest "has", we need to add count relation constraints on the
        // callback Closure. We'll do this by simply passing the Closure its own
        // reference to itself so it calls itself recursively on each segment.
        $closure = function ($q) use(&$closure, &$relations, $operator, $count, $boolean, $callback) {
            if (count($relations) > 1) {
                $q->whereHas(array_shift($relations), $closure);
            } else {
                $q->has(array_shift($relations), $operator, $count, 'and', $callback);
            }
        };
        return $this->has(array_shift($relations), '>=', 1, $boolean, $closure);
    }