首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更新集合值

更新集合值
EN

Stack Overflow用户
提问于 2022-04-05 17:55:41
回答 1查看 44关注 0票数 0

我正在尝试更新Collection中的一个列,但似乎没有什么能起作用。尝试过update$kk->amount = 100['amount'] = 100,但这些都不起作用。

代码语言:javascript
复制
$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
                ]);
            }
        });
EN

回答 1

Stack Overflow用户

发布于 2022-04-05 20:02:55

可能问题是您没有通过引用传递$newReturnBoxes

试着替换这个:

代码语言:javascript
复制
use ($newReturnBoxes)

在这方面:

代码语言:javascript
复制
use (&$newReturnBoxes)

参考资料:https://www.php.net/manual/en/language.references.pass.php

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71756345

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档