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 LazyCollection::sliding($size, $step) when $size < 1. sliding builds overlapping 'window' chunks of $size elements advancing by $step; a window size below 1 cannot produce a valid chunk and is rejected with an InvalidArgumentException. The PHPDoc marks both params as positive-int.
Solutions
- Ensure size >= 1: ->sliding(max(1, $size), max(1, $step)).
- Validate both size and step before calling.
- Provide a sensible default (size=2) when sourced from user input.
Example fix
// before $windows = $collection->sliding($windowSize, $step); // after $windowSize = max(1, $windowSize); $step = max(1, $step); $windows = $collection->sliding($windowSize, $step);
Defensive patterns
Strategy: validation
Validate before calling
if ($size < 1 || $step < 1) {
throw new \InvalidArgumentException('sliding size and step must be >= 1');
}
return $collection->sliding($size, $step); Type guard
$size = (is_int($size) && $size >= 1) ? $size : 2; $step = (is_int($step) && $step >= 1) ? $step : 1;
Try / catch
try {
return $collection->sliding($size, $step);
} catch (\InvalidArgumentException $e) {
return $collection->sliding(2, 1);
} Prevention
- Clamp both size and step: sliding(max(1,$size), max(1,$step)).
- Validate window configs at the request boundary.
- Default window size to 2 and step to 1 in config.
When it happens
Trigger: ->sliding(0), ->sliding(-1); size derived from a length/width setting that can be zero; passing a configurable window size from a request.
Common situations: Time-series windowing where window size is configurable; pagination window math that yields 0 for small datasets; defaults left at 0 in config.
Related errors
- Number of groups must be at least 1.
- Step value cannot be zero.
- Step value must be at least 1.
- Generators should not be passed directly to LazyCollection…
- Collection should only include
AI-assisted analysis of laravel/framework@e0f6eb3518 (2026-08-11).
Data as JSON: /api/errors/366743ac97778e9a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Illuminate/Collections/LazyCollection.php:1198
#[\Override]
public function shuffle()
{
return $this->passthru(__FUNCTION__, []);
}
/**
* 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.');
}
return new static(function () use ($size, $step) {
$iterator = $this->getIterator();
$chunk = [];
while ($iterator->valid()) {
$chunk[$iterator->key()] = $iterator->current();
if (count($chunk) == $size) {
yield (new static($chunk))->tap(function () use (&$chunk, $step) {
$chunk = array_slice($chunk, $step, null, true);
});
// If the $step between chunks is bigger than each chunk's $sizeView on GitHub (pinned to e0f6eb3518)