iJokerAssassin
iJokerAssassin

Reputation: 149

Remove item form laravel 5.6 collection

I want to remove items from a Laravel collection if it doesn't have a certain operation code. When I searched how to get this done on the internet I found $collection->forget() and $collection->reject(), but I cant access the collection in my code, I get an error saying $orders is undefined. What do I need to change this code to get it done?

public function lasData() {
    $orders = $this->getShopOrders();

    $orders->each(function($order) {
        $order->ShopOrderRoutingStepPlans = $order->shopOrderRoutingStepPlans;

        $order->ShopOrderRoutingStepPlans->each(function($step) {
            if($step->LineNumber == 3) {
                $checkCode = substr($step->OperationCode, 0, 3);

                if($checkCode !== "LAS") {
                    //unset($step); **<- Here to forget the $order**
                }
            }
        });

        $order->Description = str_replace("Car Mats","", $order->Description);
        $order->ShopOrderMaterialPlans = $order->shopOrderMaterialPlans;
    });

The getShopOrders function is as followed:

public function getShopOrders() {
    $orders = new \Picqer\Financials\Exact\ShopOrder($this->connection);

    $results = $orders->filter('', 'ShopOrderRoutingStepPlans,ShopOrderMaterialPlans', '', array('$top' => 30));
    foreach($results as $result) {
        //Format date to legible date stamps
        $result->Created = $this->createTime($result->Created);
        $result->PlannedDate = $this->createTime($result->PlannedDate);
        $result->EntryDate = $this->createTime($result->EntryDate);
        $result->Modified = $this->createTime($result->Modified);
        $result->PlannedStartDate = $this->createTime($result->PlannedStartDate);
        $result->Item = $this->getItem($result->Item);

        //save Order
        if (!shopOrder::where('ID', '=', $result->ID)->exists()) {
           $saveOrder = shopOrder::create($this->toJson($result));
        } else {
            break;
        }

        foreach($result->ShopOrderRoutingStepPlans['results'] as $plan) {
            //Save RoutingPlan
            $routingplan = shopOrderRoutingStepPlan::create($this->toJson($plan));
        }

        foreach($result->ShopOrderMaterialPlans['results'] as $plan) {
            //Save MaterialPlan
            $routingplan = shopOrderMaterialPlan::create($this->toJson($plan));
        }

    }

    return $results;
}

Upvotes: 0

Views: 183

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

Use the filter() method:

$filteredOrders = $orders->filter(function($order) {
    $remove = false;

    $order->ShopOrderRoutingStepPlans = $order->shopOrderRoutingStepPlans;

    $order->ShopOrderRoutingStepPlans->each(function($step) use(&$remove) {
        if($step->LineNumber == 3) {
            $checkCode = substr($step->OperationCode, 0, 3);

            if($checkCode !== "LAS") {
                $remove = true;
                return false;
            }
        }
    });

    $order->Description = str_replace("Car Mats","", $order->Description);
    $order->ShopOrderMaterialPlans = $order->shopOrderMaterialPlans;

    return !$remove;
});

Upvotes: 1

Related Questions