{"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/bd6b5437e6ad87bb49f9b426724f07a9f64e9683/src/Illuminate/Collections/Arr.php#L960-L996","documentation":"Thrown by Arr::random() when the requested $number exceeds count($array). The method checks the requested count against the array size before drawing random keys, so it fails fast rather than returning fewer items than asked for.","triggerScenarios":"Calling Arr::random([1,2,3], 5); or $collection->random(count() + 1).","commonSituations":"Dynamic count derived from user input or another collection's size without clamping; calling random() on an empty or short list; off-by-one when passing total() instead of total()-1.","solutions":["Clamp the count to the array size: Arr::random($arr, min($n, count($arr))).","Validate/guard before calling: if ($n <= count($arr)) { ... }.","Use Arr::random($arr) (no count) when you only need a single item.","Handle empty input explicitly before random() since empty arrays short-circuit."],"exampleFix":"// before\n$picks = Arr::random($pool, $requested);\n\n// after\n$picks = Arr::random($pool, min($requested, count($pool)));\n// or guard\nif (count($pool) === 0) { return []; }\n$picks = Arr::random($pool, min($requested, count($pool)));","handlingStrategy":"validation","validationCode":"$count = count($pool);\n$request = min($requested, $count);\nif ($count === 0 || $request <= 0) {\n    return [];\n}\n$picks = Arr::random($pool, $request);","typeGuard":"function canRandom(array $array, int $number): bool {\n    return $number >= 1 && $number <= count($array);\n}","tryCatchPattern":"try {\n    $picks = Arr::random($pool, $requested);\n} catch (\\InvalidArgumentException $e) {\n    $picks = Arr::random($pool); // fall back to a single item\n}","preventionTips":["Clamp requested counts with min($n, count($array)).","Handle empty arrays before calling Arr::random().","Validate user-supplied 'take' inputs."],"tags":["collections","arrays","validation","laravel"],"analyzedSha":"bd6b5437e6ad87bb49f9b426724f07a9f64e9683","analyzedAt":"2026-08-06T00:28:32.783Z","schemaVersion":2}