2016-10-21 26 views
0

我有以下的循環,我嘗試添加一個新值:當我打印的$pro->sig()輸出如何在集合laravel中添加新值?

foreach ($pro->sig()->get() as $key => $sig) { 
      $sig->val = 2; 
     } 

我沒有新的價值$sig->val

+0

你試過$ sig-> attributes ['val'] = 2嗎? – Vuer

回答

3

如果你有一個集合,你可以使用pushput方法。

實施例使用PUT:

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

$collection->put('test', 'test'); 

$collection->all(); 

的輸出將是:

['product_id' => 1, 'name' => 'Desk', 'test' => 'test'] 

實施例與推:

$collection = collect([1, 2, 3, 4]); 

$collection->push(5); 

$collection->all(); 

輸出:

[1, 2, 3, 4, 5] 

參考:https://laravel.com/docs/5.3/collections#method-push

0

在我的例子,我試圖像下面

foreach ($user->emails as $key => $email) { 
    $email->test = "test"; 
} 
return $user->emails; 

它輸出等,

{ 
    "id": 76, 
    "user_id": 5, 
    "additional_email": "[email protected]", 
    "test": "test" 
    } 

請嘗試這樣。

相關問題