我正在尝试更新Collection中的一个列,但似乎没有什么能起作用。尝试过update、$kk->amount = 100和['amount'] = 100,但这些都不起作用。
$newReturnBoxes = collect([
{
"boxable_id": 2,
"box_id": 1,
"boxable_type": "return",
"amount": 3,
"created_at": "2022-03-29T08:29:03.000000Z",
"updated_at": "2022-03-29T08:29:03.000000Z"
},
{
"boxable_id": 3,
"box_id": 1,
"boxable_type": "return",
"amount": 4,
"created_at": "2022-03-29T08:32:02.000000Z",
"updated_at": "2022-03-29T08:32:02.000000Z"
},
{
"boxable_id": 3,
"box_id": 2,
"boxable_type": "return",
"amount": 2,
"created_at": null,
"updated_at": null
}
]);
$returnBoxes->each(function ($trx) use ($newReturnBoxes) {
$amount = $trx->amount;
$boxId = $trx->box_id;
if ($newReturnBoxes->contains('box_id', $boxId)) {
// Already contains key, increment amount
$kk = $newReturnBoxes->firstWhere('box_id', $boxId);
$kk['amount'] = 100; // actually want to increase prev amount with current amount
} else {
$newReturnBoxes->push([
'box_id' => $boxId,
'amount' => $amount
]);
}
});发布于 2022-04-05 20:02:55
可能问题是您没有通过引用传递$newReturnBoxes。
试着替换这个:
use ($newReturnBoxes)在这方面:
use (&$newReturnBoxes)参考资料:https://www.php.net/manual/en/language.references.pass.php
https://stackoverflow.com/questions/71756345
复制相似问题