#Unable to chunk results into a closure from my Contact model

12 messages · Page 1 of 1 (latest)

remote crystal
#

I am attempting to get filtered records from my Contact model based on the unsusbcribe column being set to 0 or null and then take a subset of those records and chunk 8 of those at a time into a closure - this is the following code I have tried:

$contacts = Contact::where('unsubscribed', 0)
                ->orWhereNull('unsubscribed')
            ->inRandomOrder()->take(40)->get();

            $contacts->chunk(8, function ($chunk) {
                dump('Processing batch...');
                dump($chunk->toArray());
                dump('Random dump process executed.');
            });

However the dump does not get executed, since I have all ready confirmed and tested there are enough records to return to justify the chunk what am I doing wrong and please keep in mind I am doing this inside a Feature test from the terminal.

UPDATE: Have also tried:

Contact::where('unsubscribed', 0)
    ->orWhereNull('unsubscribed')
    ->take(40)
    ->chunk(8, function (Collection $contacts) {
        dump("We on!!!");
    });

as per suggestion of @paper fjord

Thank you!

https://p147.p4.n0.cdn.getcloudapp.com/items/GGu7qq9z/aaed0cea-05b5-4655-ae97-6e12f48434ad.mp4?v=fc0aa520b4c3b4e1b48d13e7d8f551d0

paper fjord
#

Stop trying to pull random records, that will never end well when you are expecting an actual outcome. If you need to perform something and there are a ton of records, then simply chunk directly on your query constraint, or look into lazy/cursor as well.

https://laravel.com/docs/10.x/eloquent#chunking-using-lazy-collections
https://laravel.com/docs/10.x/eloquent#cursors
If I were you, I'd probably have one job dispatched, that ran the chunking 10-100 (whatever is reasonable for a single job to handle the actual task), dispatching more jobs with each chunk.

remote crystal
paper fjord
#
class FirstJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public function handle()
    {
        Contact::query()
            ->select('id')
            ->where('unsubscribed', 0)
            ->orWhereNull('unsubscribed')
            ->chunk(50, function (Collection $contacts) {
                SecondJob::dispatch($contacts)
            });
    }
}

class SecondJob implements ShouldQueue
{
    use Dispatchable;
    use InteractsWithQueue;
    use Queueable;
    use SerializesModels;

    public function __construct(
        public Collection $contacts
    ){}

    public function handle()
    {
        $this->contacts->each(function (Contact $contact) {
            //do something
        });
    }
}
remote crystal
#

I appreciate the code - never considered dispatching from second job

paper fjord
#

All for me tonight, hope you get it sorted.

remote crystal
#

I'm trying limit() too

paper fjord
#

take is limit

remote crystal
#

Good night and appreciate the input 🙂