Mis*_*rPi 5 php laravel laravel-5 laravel-5.3
我有以下循环,我尝试添加一个新值:
foreach ($pro->sig()->get() as $key => $sig) {
$sig->val = 2;
}
Run Code Online (Sandbox Code Playgroud)
当我打印输出$pro->sig()
我没有新值$sig->val
Chr*_*oni 15
如果您有一个集合,您可以使用push
或put
方法.
放置示例:
$collection = collect(['product_id' => 1, 'name' => 'Desk']);
$collection->put('test', 'test');
$collection->all();
Run Code Online (Sandbox Code Playgroud)
输出将是:
['product_id' => 1, 'name' => 'Desk', 'test' => 'test']
Run Code Online (Sandbox Code Playgroud)
推送示例:
$collection = collect([1, 2, 3, 4]);
$collection->push(5);
$collection->all();
Run Code Online (Sandbox Code Playgroud)
输出:
[1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
参考:https://laravel.com/docs/5.3/collections#method-push
在我的示例中,我尝试如下所示
foreach ($user->emails as $key => $email) {
$email->test = "test";
}
return $user->emails;
Run Code Online (Sandbox Code Playgroud)
它输出像
{
"id": 76,
"user_id": 5,
"additional_email": "test@test.com",
"test": "test"
}
Run Code Online (Sandbox Code Playgroud)
请尝试这样。