php/laravel能否批量更新()

php/laravel能否批量更新(),php,mysql,laravel,Php,Mysql,Laravel,假设我正在创建一个银行管理系统。 我想激活一些用户的帐户。所以我可以 $ids = [1, 2, 3]; User::where(function($enq) use($ids) { foreach ($ids as $id) $enq->orwhere('id', $id); })->update(['activated'=>true]); 但是让他们的钱自动增加10美元怎么样 $ids = [1, 2, 3]; User::where(fu

假设我正在创建一个银行管理系统。 我想激活一些用户的帐户。所以我可以

$ids = [1, 2, 3];
User::where(function($enq) use($ids) {
    foreach ($ids as $id)
        $enq->orwhere('id', $id);
})->update(['activated'=>true]);
但是让他们的钱自动增加10美元怎么样

    $ids = [1, 2, 3];
User::where(function($enq) use($ids) {
    foreach ($ids as $id)
        $enq->orwhere('id', $id);
})->update(function(){
    //what should I write here??? maybe I can write
    //$this_obj->money+=10;
});
最大的问题是,我无法在update()中获取“当前对象”。 我的英语不是很好,我希望我的描述不会让任何人感到困惑。
我希望得到一个好的解决方案。谢谢

查询生成器包含一个增量方法,可以处理此问题:

User::whereIn('id', $ids)->increment('money', 10);

请注意,我还使用了where方法作为orwhere调用循环的简单替换。

Laravel Eloquent模型提供了按给定数量递增列值的递增方法:`

您可以做什么:

$ids = [1, 2, 3];
User::where(function($enq) use($ids) {
    foreach ($ids as $id)
        $enq->orwhere('id', $id);
})->increment('money', 10);
或者更好:

$ids = [1, 2, 3];
User::whereIn('id', $ids)
    ->increment('money', 10);

首先,感谢您的解决方案!我可以问更多的问题吗……如果“钱”不是一个整数,而是一个字符串。例如,[“100”,“200”],这是一个由json编码的字符串,表示用户有2个帐户。如何使字符串变成[“110”,“210”]首先,感谢您的解决方案!我可以问更多的问题吗……如果“钱”不是一个整数,而是一个字符串。例如,[“100”,“200”],这是一个由json编码的字符串,表示用户有2个帐户。如何使字符串变为[“110”、“210”]