Laravel:在处理数据后使用外部变量?

我有一个函数,我从数据库中获取订单,然后使用它们一次转到10个。如果我有100个订单,我相信会发生的流程是get将被调用10次(因为有100个订单)。

现在,一旦完成,我希望能够访问 $totalOrders.这可能吗?getUnfilledOrderschunkcheckStatuscheckStatusgetUnfulfilledOrders


protected function getUnfulfilledOrders()

{

    Order::where('order_status', '!=', true)

        ->where('tracking_number', '!=', null)

        ->limit(3000)

        ->chunk(10, function ($unfulfilledOrders) {

            $this->checkStatus($unfulfilledOrders);

        });


   // how to do something now with $totalOrders once ALL Orders are processed 10 at a time;


}


protected function checkStatus($unfilledOrders)

{

    $totalOrders = array();


    foreach ($unfulfilledOrders as $unfulfilledOrder) {

         // logic here

         array_push($totalOrders, $unfulFilledOrder->id);

    }

}


白衣染霜花
浏览 114回答 1
1回答

墨色风雨

喜欢这个:protected function getUnfulfilledOrders(){    $totalOrders = [];    Order::where('order_status', '!=', true)        ->where('tracking_number', '!=', null)        ->limit(3000)        // Add use (&$totalOrders)        ->chunk(10, function ($unfulfilledOrders) use (&$totalOrders) {            $totalOrders = array_merge($totalOrders, $this->checkStatus($unfulfilledOrders));        });   // how to do something now with $totalOrders once ALL Orders are processed 10 at a time;}protected function checkStatus($unfilledOrders){    $totalOrders = array();    foreach ($unfulfilledOrders as $unfulfilledOrder) {         // logic here         array_push($totalOrders, $unfilledOrder->id);    }    // Return the generated array    return $totalOrders;}在这里,我在开始时启动了一个空数组,并将 返回的任何内容合并到其中。getUnfulfilledOrders()checkStatus()
打开App,查看更多内容
随时随地看视频慕课网APP