【问题标题】:laravel clear and item in collection datalaravel clear 和 item in collection data
【发布时间】:2016-11-16 04:44:16
【问题描述】:

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

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

【问题讨论】:

  • 你能告诉我们$data中包含的数据吗
  • 不幸的是,数据是机密的,但它看起来像一个普通的集合对象,我得到了产品

标签: laravel


【解决方案1】:

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

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

【讨论】:

  • 这会将整个 $item 替换为一个空数组。产品对象可用但不完整返回。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多