2017-01-01 189 views
3

我有两个Laravel集合。宓第一个系列叫做$ custom_product合并字段laravel集合

Collection {#313 ▼ 
    #items: array:1 [▼ 
    0 => array:1 [▼ 
     "custom_description" => "insert description here" 
    ] 
    ] 
} 

我有我的其他集合称为$ api_product

Collection {#311 ▼ 
    #items: array:29 [▼ 
    "article_id" => 5570 
    "active" => null 
    "name" => "CORBATA POLIESTER" 
    "detail" => "- Pesa: 43 gr." 
    "constructor_name" => "gift" 
    "stock_available" => true 
    "stock" => null 
    "prices" => array:6 [▶] 
    "price_pvp" => 29.0 
    "size" => "1410X100" 
    ] 
} 

我想这个结果。同样的集合,但与现场custom_description补充说:

Collection {#311 ▼ 
    #items: array:29 [▼ 
    "article_id" => 5570 
    "active" => null 
    "custom_description" => "insert description here"//add this field 
    "name" => "CORBATA POLIESTER" 
    "detail" => "- Pesa: 43 gr." 
    "constructor_name" => "gift" 
    "stock_available" => true 
    "stock" => null 
    "prices" => array:6 [▶] 
    "price_pvp" => 29.0 
    "size" => "1410X100" 
    ] 
} 

回答

0

您可以使用的 laravel collectionput方法。

$collection = collect(['product_id' => 1, 'name' => 'Desk']); 

$collection->put('price', 100); 

$collection->all(); 

// ['product_id' => 1, 'name' => 'Desk', 'price' => 100] 
0

可以itarete在集合和手动添加字段,以简单的数组:

for ($i = 0; $i < count($secondCollection); $i++) { 
    // Add field to an item. 
    $secondCollection[$i]['custom_description'] = $firstCollection[$i]['custom_description']; 
} 

这个代码将会给一个想法。实际的代码实际上取决于集合结构。

0

在这种情况下,你是每单个记录使用给出的例子这样做,我认为这将是:

$api_product->put($custom_product->toArray()[0]); 

如果你想多个记录相结合,你可以使用map()方法

$api_products_collection->map(function($key,$item) { 
    //combine the record 
    return $item; 
});