diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 3ccd32edb4bb..0f57bdb33504 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1421,7 +1421,24 @@ public function sortKeysUsing(callable $callback) public function take($limit) { if ($limit < 0) { - return $this->passthru('take', func_get_args()); + return new static(function () use ($limit) { + $queue = []; + $positiveLimit = abs($limit); + + $iterator = $this->getIterator(); + foreach ($iterator as $key => $value) { + $queue[] = [$key, $value]; + + // If the length of tempQueue exceeds abs($limit), remove the first element. + if (count($queue) > $positiveLimit) { + array_shift($queue); + } + } + + foreach ($queue as $item) { + yield $item[0] => $item[1]; + } + }); } return new static(function () use ($limit) {