Skip to content

Commit 935b090

Browse files
Add note about logically grouping "or" queries for chunkById and lazyById (#9921)
* Update queries.md * Update eloquent.md * Update eloquent.md * Update queries.md --------- Co-authored-by: Taylor Otwell <taylor@laravel.com>
1 parent b967f72 commit 935b090

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

eloquent.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,19 @@ Flight::where('departed', true)
482482
}, $column = 'id');
483483
```
484484

485+
Since the `chunkById` and `lazyById` methods add their own "where" conditions to the query being executed, you should typically [logically group](/docs/{{version}}/queries#logical-grouping) your own conditions within a closure:
486+
487+
```php
488+
Flight::where(function ($query) {
489+
$query->where('delayed', true)->orWhere('cancelled', true);
490+
})->chunkById(200, function (Collection $flights) {
491+
$flights->each->update([
492+
'departed' => false,
493+
'cancelled' => true
494+
]);
495+
}, column: 'id');
496+
```
497+
485498
<a name="chunking-using-lazy-collections"></a>
486499
### Chunking Using Lazy Collections
487500

queries.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,20 @@ If you are updating database records while chunking results, your chunk results
161161
}
162162
});
163163

164+
Since the `chunkById` and `lazyById` methods add their own "where" conditions to the query being executed, you should typically [logically group](#logical-grouping) your own conditions within a closure:
165+
166+
```php
167+
DB::table('users')->where(function ($query) {
168+
$query->where('credits', 1)->orWhere('credits', 2);
169+
})->chunkById(100, function (Collection $users) {
170+
foreach ($users as $user) {
171+
DB::table('users')
172+
->where('id', $user->id)
173+
->update(['credits' => 3]);
174+
}
175+
});
176+
```
177+
164178
> [!WARNING]
165179
> When updating or deleting records inside the chunk callback, any changes to the primary key or foreign keys could affect the chunk query. This could potentially result in records not being included in the chunked results.
166180

0 commit comments

Comments
 (0)