laravel/framework · error · InvalidArgumentException

Number of shifted items may not be less than zero.

Error message

Number of shifted items may not be less than zero.

What it means

Thrown by Collection::shift($count) when $count < 0. shift() removes and returns the first N items; a negative count has no meaningful semantics, so it is rejected. Zero returns an empty collection, one returns a single value, and a positive int returns a new collection.

Source

Thrown at src/Illuminate/Collections/Collection.php:1271

        if ($position === $keys->count() - 1) {
            return null;
        }

        return $this->get($keys->get($position + 1));
    }

    /**
     * Get and remove the first N items from the collection.
     *
     * @param  int<0, max>  $count
     * @return ($count is 1 ? TValue|null : static<int, TValue>)
     *
     * @throws \InvalidArgumentException
     */
    public function shift($count = 1)
    {
        if ($count < 0) {
            throw new InvalidArgumentException('Number of shifted items may not be less than zero.');
        }

        if ($this->isEmpty()) {
            return null;
        }

        if ($count === 0) {
            return $this->newInstance();
        }

        if ($count === 1) {
            return array_shift($this->items);
        }

        $results = [];

        $collectionCount = $this->count();

View on GitHub (pinned to bd6b5437e6)

Solutions

  1. Clamp the count: $collection->shift(max(0, $count)).
  2. Validate before calling: if ($count >= 0) { ... }.
  3. Re-check the arithmetic producing the count.
  4. Branch: if the value is negative, treat the input as empty rather than shifting.

Example fix

// before
$head = $collection->shift($count);

// after
$head = $collection->shift(max(0, (int) $count));
Defensive patterns

Strategy: validation

Validate before calling

$count = max(0, (int) $count);
$head = $collection->shift($count);

Type guard

function isValidShiftCount(int $count): bool {
    return $count >= 0;
}

Try / catch

try {
    $head = $collection->shift($count);
} catch (\InvalidArgumentException $e) {
    $head = $collection->shift(0);
}

Prevention

When it happens

Trigger: Calling $collection->shift(-1) or passing a computed count that goes negative.

Common situations: Count derived from subtraction that can underflow (e.g. count() - offset where offset > count); user input not validated; arithmetic on an empty collection's size.

Related errors


AI-assisted analysis of laravel/framework@bd6b5437e6 (2026-08-06). Data as JSON: /data/errors/85379715c74d186b.json. Report an issue: GitHub.