lastdragon-ru / lara-asp-eloquent
The Awesome Set of Packages for Laravel - Eloquent Helpers.
Requires
- php: ^8.2|^8.3
- illuminate/collections: ^10.34.0|^11.0.0
- illuminate/database: ^10.34.0|^11.0.0
- illuminate/support: ^10.34.0|^11.0.0
- lastdragon-ru/lara-asp-core: 7.0.0
- symfony/polyfill-php83: ^1.28
Requires (Dev)
- ext-pdo_sqlite: *
- illuminate/contracts: ^10.34.0|^11.0.0
- lastdragon-ru/lara-asp-testing: 7.0.0
- mockery/mockery: ^1.6.5
- orchestra/testbench: ^8.0.0|^9.0.0
- phpunit/phpunit: ^10.1.0|^11.0.0
- dev-main
- 7.0.0
- 6.x-dev
- 6.4.2
- 6.4.1
- 6.4.0
- 6.3.0
- 6.2.0
- 6.1.0
- 6.0.0
- 5.x-dev
- 5.6.0
- 5.5.0
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.0
- 5.0.0-beta.1
- 5.0.0-beta.0
- 4.x-dev
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.0
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.0.0
- 2.x-dev
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.15.0
- 0.14.1
- 0.14.0
- 0.13.0
- 0.12.0
- 0.11.0
- 0.10.0
- 0.9.0
- 0.8.1
- 0.8.0
- 0.7.0
- 0.6.1
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
This package is auto-updated.
Last update: 2024-11-17 07:57:57 UTC
README
This package contains useful extensions and mixins for Eloquent.
Requirements
Installation
composer require lastdragon-ru/lara-asp-eloquent
Iterators
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()
it is always safe (please see laravel/framework#35400 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) { // ... }
Mixins
\Illuminate\Database\Eloquent\Builder
Upgrading
Please follow Upgrade Guide.
Contributing
This package is the part of Awesome Set of Packages for Laravel. Please use the main repository to report issues, send pull requests, or ask questions.