2016-11-16 25 views
0

我正在使用Laravel 5.3并进行查询,但需要在返回之前修改集合的数据结构。我试图通过将它们设置为一个空数组来清除这些命令,但它不清除(我仍然在返回的json中获得所有原始结果)。但是,如果我取消了它的工作顺序。事实上,无论我设置$ res-> product->命令,它都不起作用,除非我先将其解除。laravel清除并收集数据中的项目

$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; 
     } 
+0

你能告诉我们包含在$数据中的数据 –

+0

不幸的是,数据是保密的,但它看起来像一个普通的集合对象,我得到的产品 – Jongo

回答

0

你可以尝试集合任意变换laravel 5.3将同时在查询bulider返回collection,雄辩

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

这将用一个空数组替换整个$ item。产品对象可用,但不能返回。 – Jongo