lundi 10 octobre 2016

Laravel Eloquent chunk doesn't work if save used in callback

I'm trying to run the following:

$inventory = Inventory::whereIn('id', $ids);

$inventory->chunk(20, function (\Illuminate\Database\Eloquent\Collection $chunk) {
    $skus = $chunk->pluck('sku')->toArray();

    $pricing = $this->getPricing($skus);

    foreach ($pricing as $p) {
        $inv = $chunk->where('sku', $p['sku'])->first();

        if (!empty($inv)) {
            $inv->price = $p['price'];
            $inv->checked_at = Carbon::now();
            $inv->save();
        }
    }
});

When I do so, it never processings anything past this first chunk even if there are hundreds of items in the result.

If I remove $inv->save() from within the callback, then it finishes processing the rest of the chunks. However, this completely defeats the purpose.

Is this a bug or do I need to do something completely different to get this to work like I'd expect?

Laravel 5.3.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire