mongodb/laravel-mongodb · error · ArgumentCountError

Too few arguments to function

Error message

Too few arguments to function %s(%s), 1 passed and at least 2 expected when the 1st is not an array or a callable

What it means

where() requires at least 2 arguments unless the first argument is an array of columns/conditions or a callable sub-query builder. A single non-array, non-callable argument is ambiguous and rejected with an ArgumentCountError naming the actual column value.

Solutions

  1. Provide the full triple: ->where('email', '=', $value) or shorthand ->where('email', $value).
  2. If checking for null use ->whereNull('email') or ->where('email', '=', null) with explicit operator.
  3. Guard dynamically-built calls: only call where() when count($args) >= 2 or is_array($args[0]).
  4. Use ->where($conditions) with an array form for dynamic condition sets.

Example fix

// before
$query->where('status');
// after
$query->where('status', '=', 'active');
Defensive patterns

Strategy: type-guard

Validate before calling

if (!is_array($column) && !is_callable($column) && func_num_args() < 2) {
    throw new InvalidArgumentException('where() needs column + value');
}

Type guard

function canCallWhere(mixed ...$args): bool {
    return count($args) >= 2 || is_array($args[0] ?? null) || is_callable($args[0] ?? null);
}

Prevention

When it happens

Trigger: ->where('email') (no operator/value); ->where(null); typo'd call where the operator/value were dropped during refactoring; dynamic argument spreading where func_get_args produced one element.

Common situations: Refactoring leftovers; building conditions from arrays that turned out empty; misuse of where() intending ->whereNotNull().

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of mongodb/laravel-mongodb@0634653039 (2026-09-15). Data as JSON: /api/errors/3f048028af465c25. Report an issue: GitHub.

Appendix: source

Thrown at src/Query/Builder.php:1310

        // Remove the leading $ from operators.
        if (func_num_args() >= 3) {
            $operator = &$params[1];

            if (is_string($operator) && str_starts_with($operator, '$')) {
                $operator = substr($operator, 1);
            }

            if ($operator === self::UNSAFE_FIELD_QUERY) {
                $operator = '=';
            } elseif ($operator === '=' && self::firstOperatorKey($params[2]) !== null) {
                $this->throwIfIdLikeOperatorValue($params[0], $params[2]);

                $params[2] = ['$eq' => $params[2]];
            }
        }

        if (func_num_args() === 1 && ! is_array($column) && ! is_callable($column)) {
            throw new ArgumentCountError(sprintf('Too few arguments to function %s(%s), 1 passed and at least 2 expected when the 1st is not an array or a callable', __METHOD__, var_export($column, true)));
        }

        if (is_float($column) || is_bool($column) || $column === null) {
            throw new InvalidArgumentException(sprintf('First argument of %s must be a field path as "string". Got "%s"', __METHOD__, get_debug_type($column)));
        }

        return parent::where(...$params);
    }

    /** Array-of-wheres calls are marked so their generated "=" keeps building an operator document. */
    #[Override]
    protected function addArrayOfWheres($column, $boolean, $method = 'where')
    {
        return $this->whereNested(function ($query) use ($column, $method, $boolean) {
            foreach ($column as $key => $value) {
                if (is_numeric($key) && is_array($value)) {
                    $query->{$method}(...array_values($value), boolean: $boolean);
                } else {

View on GitHub (pinned to 0634653039)