laravel clear和收集数据中的项目

时间:2016-11-16 04:44:16

标签: laravel

我使用的是Laravel 5.3并且有一个查询但需要在返回之前修改集合的数据结构。我试图通过将它们设置为空数组来清除命令,但它并不清楚(我仍然在返回的json中得到所有原始结果)。但是,如果我取消订单,它的工作原理。事实上,无论我设置$ res-> product->订单,它都不起作用,除非我先取消它。

$stores = array();    

foreach($data as $key => $res) {

           // This is a hacky way to clear the orders but it works
           $orders = array();
           unset($res->product->orders);
           $res->product->orders = $orders;

           // This is the way I would have done it but it doesn't work
           $res->product->orders = array();

           $stores[$res->store_id]['name'] = $res->product->store_name;
           $stores[$res->store_id]['data'][] = $res;
        }

1 个答案:

答案 0 :(得分:0)

您可以尝试收集转换任何laravel 5.3将在两个查询bulider中返回collection,éloquent

$data->transform(function($item) {
    return $item->product->orders = []; //I'm not sure here Product is object here?
});