jlmmns's avatar
Level 12

How to use chunkById with a custom order?

Hi all!

Currently, it seems it's not possible to use chunkById with a custom order, other than by id.

Has anyone of you figured out a workaround?

0 likes
4 replies
ftiersch's avatar

Well, that's the whole point of it isn't it?

Do you really need chunkById or is chunk enough?

jlmmns's avatar
Level 12

The whole point of it is to not skip any records when the query's where clauses get affected.

So I'm required to use chunkById, instead of just chunk.

I went ahead with a workaround using a do-while loop and keeping track of the processed id's myself. 👍

2 likes
Abi's avatar

@dirksamson if you have a where and you do an update on the where column. example:

User::where('active', 1)->chunk(100, function ($users) {
  $users->each(function ($user) {
    $user->update('active', 0);
  });
});
1 like

Please or to participate in this conversation.