Eloquent chunk() missing half the results

后端 未结 3 550
粉色の甜心
粉色の甜心 2021-01-31 05:18

I have a problem with Laravel\'s ORM Eloquent chunk() method. It misses some results. Here is a test query :

$destinataires = Destinataire::where(\'statut\', \'&         


        
3条回答
  •  余生分开走
    2021-01-31 05:50

    For anyone looking for a bit of code that solves this, here you go:

    while (Model::where('x', '>', 'y')->count() > 0)
    {
        Model::where('x', '>', 'y')->chunk(10, function ($models)
        {
            foreach ($models as $model)
            {
                $model->delete();
            }
        });
    }
    

    The problem is in the deletion / removal of the model while chunking away at the total. Including it in a while loop makes sure you get them all! This example works when deleting Models, change the while condition to suit your needs!

提交回复
热议问题