The Awesome Set of Packages for Laravel - Eloquent Helpers.

0.10.0 2021-12-25 17:36 UTC

This package is auto-updated.

Last update: 2022-01-25 17:48:45 UTC


This package is the part of Awesome Set of Packages for Laravel.

Read more.

This package contains useful extensions and mixins for Eloquent.


composer require lastdragon-ru/lara-asp-eloquent


Iterators are similar to Builder::chunk() but uses generators instead of \Closure that makes code more readable:

$query = \App\Models\User::query();

$query->chunk(100, function ($users) {
    foreach ($users as $user) {
        // ...

foreach ($query->getChunkedIterator() as $user) {
    // ...

Iterators also support limit/offset, by default it will try to get them from the Builder, but you can also set them by hand:

$query = \App\Models\User::query()->offset(10)->limit(20);

foreach ($query->getChunkedIterator() as $user) {
    // ... 20 items from 10 

foreach ($query->getChunkedIterator()->setOffset(0) as $user) {
    // ...20 items from 0

When you use the default ChunkedIterator you should not modify/delete the items while iteration or you will get unexpected results (eg missing items). If you need to modify/delete items while iteration you can use ChunkedChangeSafeIterator that specially created for this case and unlike standard chunkById() is always safe (please see for more details). But there are few limitations:

  • it is not possible to sort rows, they always will be sorted by column asc;
  • the column should not be changed while iteration or this may lead to repeating row in results;
  • the row inserted while iteration may be skipped if it has column with the value that lover than the internal pointer;
  • queries with UNION is not supported;
  • offset from Builder will not be used;

To create a change safe instance you can use:

$query = \App\Models\User::query();

foreach ($query->getChangeSafeIterator() as $user) {
    // ...



Name Description
getChunkedIterator() Return ChunkedIterator instance.
getChangeSafeIteratorIterator() Return ChunkedChangeSafeIterator instance.


Same as \Illuminate\Database\Query\Builder and

Name Description
orderByKey(string $direction = 'asc') Add an ORDER BY primary_key clause to the query.
orderByKeyDesc() Alias of orderByKey('desc')