{"id":"82035ed9e54d3a36","repo":"laravel/framework","slug":"non-associative-array-passed-to-decrementeach-meth","errorCode":null,"errorMessage":"Non-associative array passed to decrementEach method.","messagePattern":"Non-associative array passed to decrementEach method\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Builder.php","lineNumber":4494,"sourceCode":"        return $this->decrementEach([$column => $amount], $extra);\n    }\n\n    /**\n     * Decrement 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 decrementEach(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 decrement amount for column: '$column'.\");\n            } elseif (! is_string($column)) {\n                throw new InvalidArgumentException('Non-associative array passed to decrementEach 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     * Delete records from the database.\n     *\n     * @param  mixed  $id\n     * @return int\n     */\n    public function delete($id = null)\n    {\n        // If an ID is passed to the method, we will set the where clause to check the\n        // ID to let developers to simply and quickly remove a single row from this","sourceCodeStart":4476,"sourceCodeEnd":4512,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Builder.php#L4476-L4512","documentation":"Thrown by decrementEach when a key in $columns is not a string (a sequential/list array rather than associative). decrementEach needs column-name => amount pairs; a list like [5, 10] has no column targets. The is_numeric check fires first, so a numeric-only list triggers the prior numeric error.","triggerScenarios":"`decrementEach([5, 10])`. Passing `array_values($assoc)`. Building the map with array functions that reindex numerically.","commonSituations":"Transforming an associative map through array_map/array_filter that loses keys; merging with a list; JSON decoded as a list rather than an object.","solutions":["Pass an associative array: `['stock' => 1, 'reserved' => 1]`.","Preserve keys: use `collect(...)->mapWithKeys(...)` or `array_combine`.","Decode JSON as object when the source is a key map.","Assert associativity before calling."],"exampleFix":"// before\n$amounts = array_filter($request->all(), 'is_numeric');\n$model->decrementEach(array_values($amounts));\n// => Non-associative array passed to decrementEach method.\n\n// after\n$pairs = ['stock' => $request->integer('stock', 0)];\n$model->decrementEach($pairs);","handlingStrategy":"type-guard","validationCode":"if (array_keys($columns) === range(0, count($columns) - 1)) {\n    throw new \\InvalidArgumentException('decrementEach 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":"// 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","decrement","array-shape","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}