{"record":{"id":"e621b23ecc9b6027","repo":"laravel/framework","slug":"the-seed-value-must-be-numeric","errorCode":null,"errorMessage":"The seed value must be numeric.","messagePattern":"The seed value must be numeric\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Database/Query/Grammars/MySqlGrammar.php","lineNumber":347,"sourceCode":"        return 'cast('.$value.' as json)';\n    }\n\n    /**\n     * Compile the random statement into SQL.\n     *\n     * @param  string|int  $seed\n     * @return string\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public function compileRandom($seed)\n    {\n        if ($seed === '' || $seed === null) {\n            return 'RAND()';\n        }\n\n        if (! is_numeric($seed)) {\n            throw new InvalidArgumentException('The seed value must be numeric.');\n        }\n\n        return 'RAND('.(int) $seed.')';\n    }\n\n    /**\n     * Compile the lock into SQL.\n     *\n     * @param  \\Illuminate\\Database\\Query\\Builder  $query\n     * @param  bool|string  $value\n     * @return string\n     */\n    protected function compileLock(Builder $query, $value)\n    {\n        if (! is_string($value)) {\n            return $value ? 'for update' : 'lock in share mode';\n        }\n","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Database/Query/Grammars/MySqlGrammar.php#L329-L365","documentation":"MySqlGrammar's compileRandom accepts a seed value for RAND(seed). If the seed is not empty/null (which produces RAND()) and is not numeric (fails is_numeric()), it throws an InvalidArgumentException. MySQL's RAND() accepts only an integer seed, so non-numeric seeds are rejected.","triggerScenarios":"Calling ->inRandomOrder('abc') or ->inRandomOrder($stringVar) where $stringVar is a non-numeric string. Also triggered if inRandomOrder receives an array or object.","commonSituations":"Passing a string-based seed (e.g., a UUID or session token) to inRandomOrder thinking it acts as a hash-based seed. Dynamically passing user input as a seed without validation. Passing a float string like '1.5' works (is_numeric returns true), but truly non-numeric strings fail.","solutions":["Call ->inRandomOrder() with no argument to use RAND() without a seed","Pass an integer seed: ->inRandomOrder(42) or ->inRandomOrder((int) $seed)","Validate the seed before passing: if (is_numeric($seed)) { $query->inRandomOrder($seed); } else { $query->inRandomOrder(); }"],"exampleFix":"// before — non-numeric seed throws\n$results = Product::inRandomOrder($request->input('seed', 'abc'))->get();\n\n// after — cast or default to null\n$seed = $request->input('seed');\n$results = Product::inRandomOrder(is_numeric($seed) ? (int) $seed : null)->get();","handlingStrategy":"validation","validationCode":"$seed = $request->input('seed');\nif ($seed === null || $seed === '' || ! is_numeric($seed)) {\n    $results = Product::inRandomOrder()->get();\n} else {\n    $results = Product::inRandomOrder((int) $seed)->get();\n}","typeGuard":"function isValidRandomSeed(mixed $seed): bool {\n    return $seed === null || $seed === '' || is_numeric($seed);\n}","tryCatchPattern":"try {\n    Product::inRandomOrder($seed)->get();\n} catch (\\InvalidArgumentException $e) {\n    if (str_contains($e->getMessage(), 'seed value must be numeric')) {\n        Product::inRandomOrder()->get();\n    } else {\n        throw $e;\n    }\n}","preventionTips":["Call inRandomOrder() without arguments unless you specifically need a reproducible seed","Validate user-supplied seeds with is_numeric() before passing to inRandomOrder()","Cast seeds to int: inRandomOrder((int) $seed) to satisfy MySQL's RAND(N) requirement"],"tags":["random","mysql","seed","validation"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}