mongodb/laravel-mongodb · error · InvalidArgumentException

2nd argument of () must be "null" when 1st argument is an…

Error message

2nd argument of %s() must be "null" when 1st argument is an array. Got "%s" instead.

What it means

Thrown by push()/pullAll-style batch APIs when the first argument is an array of column names but a non-null second argument is also supplied. When pushing to multiple columns, each column's value must be given inside the array itself; a scalar $value is only valid with a single string column.

Solutions

  1. If pushing to one column: pass a string column name and the value as the 2nd argument.
  2. If pushing to multiple columns: pass [$column => $value, ...] and remove the 2nd argument (pass null).
  3. Wrap the value for each key in an array when pushing lists, e.g. ->push(['tags' => ['php']]).

Example fix

// before
$query->push(['tags', 'votes'], 'like');
// after
$query->push(['tags' => ['like'], 'votes' => [1]]);
Defensive patterns

Strategy: validation

Validate before calling

if (is_array($column) && $value !== null) {
    throw new InvalidArgumentException('Use [$col => $value] form and drop the 2nd argument');
}

Prevention

When it happens

Trigger: ->push(['tags', 'votes'], 'like') — array column list combined with a scalar value; correct forms are ->push('tags', 'like') (single column) or ->push(['tags' => ['a'], 'votes' => [1]]) (per-column values).

Common situations: Mass-assignment style calls where devs assume the 2nd argument applies to every column; porting code from other builders that accept a default value per column.

Related errors


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

Appendix: source

Thrown at src/Query/Builder.php:1095

     * Append one or more values to an array.
     *
     * @param  string|array $column
     * @param  mixed        $value
     * @param  bool         $unique
     *
     * @return int
     */
    public function push($column, $value = null, $unique = false)
    {
        // Use the addToSet operator in case we only want unique items.
        $operator = $unique ? '$addToSet' : '$push';

        // Check if we are pushing multiple values.
        $batch = is_array($value) && array_is_list($value);

        if (is_array($column)) {
            if ($value !== null) {
                throw new InvalidArgumentException(sprintf('2nd argument of %s() must be "null" when 1st argument is an array. Got "%s" instead.', __METHOD__, get_debug_type($value)));
            }

            $query = [$operator => $column];
        } elseif ($batch) {
            $query = [$operator => [(string) $column => ['$each' => $value]]];
        } else {
            $query = [$operator => [(string) $column => $value]];
        }

        return $this->performUpdate($query);
    }

    /**
     * Remove one or more values from an array.
     *
     * @param  string|array $column
     * @param  mixed        $value
     *

View on GitHub (pinned to 0634653039)