{"id":"0afc7c0534620984","repo":"laravel/framework","slug":"non-numeric-value-passed-to-increment-method","errorCode":null,"errorMessage":"Non-numeric value passed to increment method.","messagePattern":"Non-numeric value passed to increment method\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Builder.php","lineNumber":4431,"sourceCode":"        return $this->connection->affectingStatement(\n            $this->grammar->compileUpsert($this, $values, (array) $uniqueBy, $update),\n            $bindings\n        );\n    }\n\n    /**\n     * Increment 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 increment($column, $amount = 1, array $extra = [])\n    {\n        if (! is_numeric($amount)) {\n            throw new InvalidArgumentException('Non-numeric value passed to increment method.');\n        }\n\n        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)) {","sourceCodeStart":4413,"sourceCodeEnd":4449,"githubUrl":"https://github.com/laravel/framework/blob/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Database/Query/Builder.php#L4413-L4449","documentation":"Thrown by increment when $amount fails is_numeric(). increment generates `SET col = col + amount`; a non-numeric amount (string text, null, array, object) would produce invalid SQL or wrong bindings. Note the default is 1, so this only fires when the caller explicitly passes a bad value.","triggerScenarios":"`->increment('views', $request->views)` where views is a string like 'five' or null. Passing a numeric string is fine (is_numeric accepts '5'), but letters fail. Passing a float cast of a non-numeric string yields 0, which is numeric, so the real danger is unclosed form fields yielding null/empty.","commonSituations":"Counter endpoints receiving unvalidated JSON; analytics hooks incrementing by user-supplied weights; nullable columns read back as null and fed back into increment.","solutions":["Cast or validate the input: `$amount = (float) $request->input('n', 1); if (!is_numeric($request->input('n'))) abort(422);`.","Provide an explicit numeric default: `->increment('views', 1)`.","Use a numeric-string check (`is_numeric`) on raw input before calling.","Reject empty/null with a request validation rule like `'n' => 'sometimes|numeric|min:0'`."],"exampleFix":"// before\n$post->increment('views', $request->input('views'));\n// when views is null or 'lots' => Non-numeric value passed to increment method.\n\n// after\n$request->validate(['views' => 'sometimes|numeric|min:0']);\n$post->increment('views', (float) $request->input('views', 1));","handlingStrategy":"validation","validationCode":"if (! is_numeric($amount)) {\n    throw new \\InvalidArgumentException('increment amount must be numeric, got: '.get_debug_type($amount));\n}\n$model->increment($column, (float) $amount);","typeGuard":"function isNumericAmount(mixed $amount): bool\n{\n    return is_int($amount) || is_float($amount) || (is_string($amount) && is_numeric($amount));\n}","tryCatchPattern":"// Validate the amount before calling; catching is rarely useful for a programming/type error.","preventionTips":["Validate request inputs with 'numeric' rules before incrementing.","Cast with a sane default: `$req->float('n', 1)`.","Reject empty/null form fields with form requests."],"tags":["query-builder","increment","numeric","invalid-argument"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}