{"record":{"id":"494348803db5fd08","repo":"laravel/framework","slug":"you-requested-requested-items-but-there-are-on","errorCode":null,"errorMessage":"You requested {$requested} items, but there are only {$count} items available.","messagePattern":"You requested (.+?) items, but there are only (.+?) items available\\.","errorType":"exception","errorClass":"InvalidArgumentException","httpStatus":null,"severity":"error","filePath":"src/Illuminate/Collections/Arr.php","lineNumber":978,"sourceCode":"\n    /**\n     * Get one or a specified number of random values from an array.\n     *\n     * @param  array  $array\n     * @param  int|null  $number\n     * @param  bool  $preserveKeys\n     * @return ($number is null ? mixed : array)\n     *\n     * @throws \\InvalidArgumentException\n     */\n    public static function random($array, $number = null, $preserveKeys = false)\n    {\n        $requested = is_null($number) ? 1 : $number;\n\n        $count = count($array);\n\n        if ($requested > $count) {\n            throw new InvalidArgumentException(\n                \"You requested {$requested} items, but there are only {$count} items available.\"\n            );\n        }\n\n        if (empty($array) || (! is_null($number) && $number <= 0)) {\n            return is_null($number) ? null : [];\n        }\n\n        $keys = (new Randomizer)->pickArrayKeys($array, $requested);\n\n        if (is_null($number)) {\n            return $array[$keys[0]];\n        }\n\n        $results = [];\n\n        if ($preserveKeys) {\n            foreach ($keys as $key) {","sourceCodeStart":960,"sourceCodeEnd":996,"githubUrl":"https://github.com/laravel/framework/blob/e0f6eb3518ac29fbbca8529e97d0df7fc9f24481/src/Illuminate/Collections/Arr.php#L960-L996","documentation":"Thrown by Arr::random() when $number (items requested) exceeds count($array). The check runs before any short-circuit for empty arrays, so even requesting 1 from an empty array triggers it.","triggerScenarios":"Calling Arr::random([], 1), Arr::random([1,2], 5), or passing a dynamic $number larger than the data set.","commonSituations":"Randomizing from a possibly-empty query result; user-supplied count parameter; off-by-one when source array was filtered down.","solutions":["Clamp $number to the array size: $n = min($n, count($array)).","Guard for emptiness: if (empty($array)) return []; before calling.","Validate the requested count against count($array) upstream."],"exampleFix":"// before\n$pick = Arr::random($rows, 3); // throws if < 3 rows\n// after\n$n = min(3, count($rows));\n$pick = $n > 0 ? Arr::random($rows, $n) : [];","handlingStrategy":"validation","validationCode":"$count = count($array);\nif ($count === 0) {\n    $pick = [];\n} else {\n    $n = min($requested, $count);\n    $pick = Arr::random($array, $n);\n}","typeGuard":"// no type guard; runtime count check\nfunction safeRandomCount(int $requested, int $available): int {\n    return max(0, min($requested, $available));\n}","tryCatchPattern":"try {\n    $pick = Arr::random($rows, 3);\n} catch (\\InvalidArgumentException $e) {\n    $pick = [];\n}","preventionTips":["Clamp the requested count to the array length.","Handle empty arrays before calling Arr::random().","Validate user-supplied counts before use."],"tags":["collections","array","validation","laravel"],"backgroundTag":null,"analyzedSha":"e0f6eb3518ac29fbbca8529e97d0df7fc9f24481","analyzedAt":"2026-08-11T20:52:37.562Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}