{"id":"77d1f4a1d62c7e5f","repo":"laravel/framework","slug":"non-associative-array-passed-to-incrementeach-meth","errorCode":null,"errorMessage":"Non-associative array passed to incrementEach method.","messagePattern":"Non-associative array passed to incrementEach method\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Builder.php","lineNumber":4452,"sourceCode":"        return $this->incrementEach([$column => $amount], $extra);\n    }\n\n    /**\n     * Increment the given column's values by the given amounts.\n     *\n     * @param  array<string, float|int|numeric-string>  $columns\n     * @param  array<string, mixed>  $extra\n     * @return int<0, max>\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function incrementEach(array $columns, array $extra = [])\n    {\n        foreach ($columns as $column => $amount) {\n            if (! is_numeric($amount)) {\n                throw new InvalidArgumentException(\"Non-numeric value passed as increment amount for column: '$column'.\");\n            } elseif (! is_string($column)) {\n                throw new InvalidArgumentException('Non-associative array passed to incrementEach method.');\n            }\n\n            $columns[$column] = $this->raw(\"{$this->grammar->wrap($column)} + $amount\");\n        }\n\n        return $this->update(array_merge($columns, $extra));\n    }\n\n    /**\n     * Decrement a column's value by a given amount.\n     *\n     * @param  string  $column\n     * @param  float|int  $amount\n     * @return int<0, max>\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function decrement($column, $amount = 1, array $extra = [])","sourceCodeStart":4434,"sourceCodeEnd":4470,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Builder.php#L4434-L4470","documentation":"Thrown by incrementEach when a key in the $columns array is not a string (i.e. the array is numerically/sequentially indexed rather than associative). incrementEach maps column-name => amount; a list like [5, 10] has no column names to target, so it is rejected. The is_numeric check runs first, so a numeric value list triggers the prior error.","triggerScenarios":"`incrementEach([5, 10])`. Passing `array_values($assoc)` which strips the keys. Building the map with `array_map` (which reindexes) instead of `array_mapWithKeys`/a comprehension.","commonSituations":"Transforming an associative map through a function that loses keys; merging maps where one side is a list; data from JSON that decoded as a list rather than an object.","solutions":["Pass an associative array: `['views' => 1, 'score' => 5]`.","Preserve keys during transforms: use `array_map` with keys via `array_combine(array_keys($a), array_map(..., $a))` or Laravel `collect(...)->mapWithKeys(...)`.","Decode JSON as object when the source is a map: `json_decode($s, true)` only if the source is an object.","Assert before calling: `if (array_keys($cols) === range(0, count($cols)-1)) throw ...`."],"exampleFix":"// before\n$amounts = array_map(fn ($v) => $v + 1, $request->all());\n$model->incrementEach(array_values($amounts));\n// => Non-associative array passed to incrementEach method.\n\n// after\n$pairs = ['views' => $request->integer('views', 0)];\n$model->incrementEach($pairs);","handlingStrategy":"type-guard","validationCode":"if (array_keys($columns) === range(0, count($columns) - 1)) {\n    throw new \\InvalidArgumentException('incrementEach expects an associative [column => amount] map.');\n}","typeGuard":"/** @param array<string, numeric> $a */\nfunction isAssociativeMap(array $a): bool\n{\n    return $a !== [] && array_keys($a) !== range(0, count($a) - 1);\n}","tryCatchPattern":"// This is a shape/type bug; validate the map before calling rather than catch.","preventionTips":["Build amount maps with collect()->mapWithKeys() to preserve keys.","Avoid array_values/array_map on the map before passing.","Decode JSON maps as objects, not lists."],"tags":["query-builder","increment","array-shape","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}