删除项目表单laravel 5.6集合

时间:2018-03-19 11:28:55

标签: laravel php-7 laravel-5.6

我想从Laravel集合中移除项目,如果它没有特定的操作代码。当我搜索如何在互联网上完成此操作时,我发现$collection->forget()$collection->reject(),但我无法在我的代码中访问该集合,我收到一条错误消息,指出$orders未定义。我需要更改此代码才能完成此操作?

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

getShopOrders函数如下:

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

1 个答案:

答案 0 :(得分:1)

使用filter()方法:

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