laravel/framework · error · InvalidArgumentException
Size value must be at least 1.
Error message
Size value must be at least 1.
What it means
Thrown by Collection::sliding($size, $step) when $size < 1. sliding() emits overlapping windows of length $size advancing by $step; a zero/negative window size is meaningless and is rejected before computing the window count.
Source
Thrown at src/Illuminate/Collections/Collection.php:1319
*/
public function shuffle()
{
return $this->newInstance(Arr::shuffle($this->items));
}
/**
* Create chunks representing a "sliding window" view of the items in the collection.
*
* @param positive-int $size
* @param positive-int $step
* @return static<int, static>
*
* @throws \InvalidArgumentException
*/
public function sliding($size = 2, $step = 1)
{
if ($size < 1) {
throw new InvalidArgumentException('Size value must be at least 1.');
} elseif ($step < 1) {
throw new InvalidArgumentException('Step value must be at least 1.');
}
$chunks = floor(($this->count() - $size) / $step) + 1;
return static::times($chunks, fn ($number) => $this->slice(($number - 1) * $step, $size));
}
/**
* Skip the first {$count} items.
*
* @param int $count
* @return static
*/
public function skip($count)
{
return $this->slice($count);View on GitHub (pinned to bd6b5437e6)
Solutions
- Clamp the size: $collection->sliding(max(1, $size), $step).
- Validate before calling: if ($size >= 1 && $step >= 1) { ... }.
- Guard for short collections: skip sliding() when count() < $size.
- Re-check the math that produced the size.
Example fix
// before
$windows = $collection->sliding($size, $step);
// after
if ($collection->count() >= $size) {
$windows = $collection->sliding(max(1, (int) $size), max(1, (int) $step));
} else {
$windows = collect();
} Defensive patterns
Strategy: validation
Validate before calling
if ($collection->count() >= 1 && $size >= 1 && $step >= 1) {
$windows = $collection->sliding($size, $step);
} else {
$windows = collect();
} Type guard
function isValidSlidingSize(int $size): bool {
return $size >= 1;
} Try / catch
try {
$windows = $collection->sliding($size, $step);
} catch (\InvalidArgumentException $e) {
$windows = collect();
} Prevention
- Clamp size with max(1, $size).
- Skip sliding() when the collection is smaller than the window.
- Validate window size derived from count()-based math.
When it happens
Trigger: Calling $collection->sliding(0), sliding(-1), or passing a computed size that resolves to 0/negative.
Common situations: Size derived from a smaller collection (e.g. sliding(count() - 1) on a 1-element collection yields 0); user-supplied window size; off-by-one in paging/window math.
Related errors
- You requested {$requested} items, but there are only {$count
- $count items were found.
- Step value must be at least 1.
- Number of shifted items may not be less than zero.
- Number of groups must be at least 1.
AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06).
Data as JSON: /data/errors/1e273424ca96f92d.json.
Report an issue: GitHub.