2017-01-01 193 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; 
});