{"record":{"id":"2b1c41e7d954acf6","repo":"laravel/framework","slug":"unable-to-cast-value-to-a-decimal","errorCode":null,"errorMessage":"Unable to cast value to a decimal.","messagePattern":"Unable to cast value to a decimal\\.","errorType":"exception","errorClass":"MathException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php","lineNumber":1541,"sourceCode":"            default => (float) $value,\n        };\n    }\n\n    /**\n     * Return a decimal as string.\n     *\n     * @param  float|string  $value\n     * @param  int  $decimals\n     * @return string\n     *\n     * @throws \\Illuminate\\Support\\Exceptions\\MathException\n     */\n    protected function asDecimal($value, $decimals)\n    {\n        try {\n            return (string) BigDecimal::of((string) $value)->toScale($decimals, RoundingMode::HalfUp);\n        } catch (BrickMathException $e) {\n            throw new MathException('Unable to cast value to a decimal.', previous: $e);\n        }\n    }\n\n    /**\n     * Return a timestamp as DateTime object with time set to 00:00:00.\n     *\n     * @param  mixed  $value\n     * @return \\Illuminate\\Support\\Carbon\n     */\n    protected function asDate($value)\n    {\n        return $this->asDateTime($value)->startOfDay();\n    }\n\n    /**\n     * Return a timestamp as DateTime object.\n     *\n     * @param  mixed  $value","sourceCodeStart":1523,"sourceCodeEnd":1559,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L1523-L1559","documentation":"Thrown as MathException (wrapping Brick\\Math\\Exception) by asDecimal() when BigDecimal::of() cannot interpret the value as a number, or when scaling fails. The decimal cast converts the value through Brick Math; any parsing error (non-numeric string, malformed input) is re-thrown as MathException with 'Unable to cast value to a decimal.'","triggerScenarios":"Casting an attribute as 'decimal:N' and assigning a non-numeric value (e.g. 'abc', an empty string, or null-like garbage), or a value BigDecimal cannot coerce. Triggered on read/write when the decimal cast runs asDecimal().","commonSituations":"Importing data with stray non-numeric tokens ('$1,234.50', 'N/A', ''); user input not validated before model assignment; a column previously stored as a free-text string now cast as decimal; locale-specific decimal separators (comma) on a system expecting dots.","solutions":["Sanitize the input to a plain numeric string before assignment (strip currency symbols, thousands separators).","Validate the value is numeric before saving (e.g. is_numeric($value)).","If the column can hold non-numeric data, remove the 'decimal' cast or store it as a string cast.","Normalize locale decimals (str_replace(',', '.', $value)) before casting."],"exampleFix":"// before\nprotected $casts = ['price' => 'decimal:2'];\n$product->price = '$1,234.50'; // MathException\n\n// after\n$product->price = (string) Number::format(1234.50); // or\n$product->price = '1234.50';","handlingStrategy":"validation","validationCode":"if (! is_numeric($value) && $value !== null) {\n    throw new \\UnexpectedValueException('Decimal cast expects numeric input, got: ' . get_debug_type($value));\n}\n$model->price = $value;","typeGuard":"function isCastableToDecimal(mixed $value): bool\n{\n    return $value === null || is_numeric($value) || (is_string($value) && is_numeric($value));\n}","tryCatchPattern":"try {\n    $model->price = $value;\n} catch (\\Illuminate\\Support\\Exceptions\\MathException $e) {\n    report($e);\n    $model->price = '0.00';\n}","preventionTips":["Sanitize user input to plain numeric strings before assigning to decimal-cast attributes.","Strip currency symbols and locale-specific separators; normalize decimal separator to '.'.","Validate with is_numeric() at the form/request layer."],"tags":["eloquent","cast","decimal","math","data-validation"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}