Laravel’s `chunkById`

So I started a new job at the beginning of the month. While I’m learning a lot, at the same time, it’s familiar because it’s mainly Laravel. At the same time, I’m always learning new gotchas with Laravel as well. Here’s a new one.

Laravel’s regular `chunk` method can have issues with FK so the chunk results differ. I had this issue where I’m chunking in a console command. I noticed I had to re-run the command several times before the task was fully complete.

To the google I went! I found a dev.to article mentioning that the results can change slightly if you just use chunk. The fix? Laravel’s chunkById. I don’t necessarily know how it works, but it does. The rest of the syntax is the same. Switching over pulled all the collection correctly and the script only ran once!

There’s your gotcha tip for Friday!