mercredi 18 janvier 2023

How to make command in Laravel that can go thousands rows and update all data with Job that can update grouped by 50 data

I want to make command that can go through all data from database and update some data in API for every row.. I am afraid of stopping command or cron and all data will not be updated. And when command run again it will go from first id. For example if I have ids: [1,2,3,4,5,6,7,8,...100,101,..1000,1001,...] I want to first update 50 rows(1-50), then (51-100)..and so on.. How can I make Job that will save from which to which ID should be updated? I hope so that someone can help me.. I tried something like this

 $woocount=WoocommerceProduct::where('sync_status', 'IN_SYNC')->count();
        
        if($woocount>100){
            $max=intval($woocount/100);
        }else{
            $max=$woocount;

        }
       // dd($max);
        $i=1;
        while($i<=$woocount){
            dump($max);
            $wooproducts=WoocommerceProduct::where([['sync_status', 'IN_SYNC'],['id','>=',$i], ['id', '<=', $max]])->get();
            dump($wooproducts->pluck('id')->toArray());
            CheckStockJob::dispatch($wooproducts);
            $i=$max+1;
            $max=$max+100;
        }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire