2016-11-16 3 views
0

私はLaravel 5.3を使用していますが、クエリを持っていますが、コレクションのデータ構造を変更する必要があります。空の配列に設定して注文をクリアしようとしていますが、クリアされません(返されたjsonの元の結果はすべて取得しています)。しかし、私が注文を解除すると、それは動作します。実際、私が$ res-> product-> ordersを何に設定しても、最初にそれを設定しない限り、うまくいきません。laravel clearとcollection dataの項目

$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

はあなたが私たちに$データに含まれるデータを表示することができ、任意のlaravel 5.3は、両方のクエリbuliderにcollectionを返します変換コレクションを試すことができます –

+0

残念ながらデータは機密ですが、それは次のようになります正規のコレクションオブジェクトと私は製品を得る – Jongo

答えて

0

あなたは雄弁

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

これは、$ item全体を空の配列に置き換えます。製品オブジェクトは使用可能ですが、未知の状態で返されません。 – Jongo

関連する問題