0

I have a function getUnfilledOrders where I get Orders from the database and then use chunk to have them go to checkStatus 10 at a time. If I have 100 orders, the flow I believe would happen is checkStatus get will get called 10 times (since there are 100 orders).
Now once that completes, I want to have access to $totalOrders in getUnfulfilledOrders. Is this possible?

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);
    }
}

1 Answer 1

1

Like this:

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;
}

Here I initiated an empty array at the start of getUnfulfilledOrders() and merged anything returned by checkStatus() into it.

More on use ($var)

More on passing by reference (&$var)

Sign up to request clarification or add additional context in comments.

3 Comments

ah smart approach, thanks! What does // Add use (&$totalOrders) mean? I will definitely use this approach but just curious if another way to do it would be by creating a global variable? I come from a JS background but I assume PHP has this?
The callbacks in PHP don't have access to outside variables by itself. You need to add use ($var) in order to be able to access a var from outside scope. But by default use ($var) passes in a "copy" of the variable, so doing anything with it wouldn't have an effect on the variable outside of the callback. Prefixing it with & however, passes the variable by reference so that any changes made within the callback will change the variable outside of the callback as well.
great explanation , Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.