Laravel:更好的更新数据库的方法(一根衬垫)?

时间:2018-08-31 22:58:37

标签: laravel

因此,我正在寻找对MySQL数据库进行更新的方法,这是我当前的更新代码:

    $currentMinute = Carbon::now();
    $nextMinute = $currentMinute->copy()->addMinute();

    $transfersWithinRange = Account::whereBetween('next_vc_transfer', [$currentMinute, $nextMinute])->get(); 

    if (count($transfersWithinRange) > 0) {
        foreach ($transfersWithinRange as $transfer) {
            $transfer->balance = $transfer->balance + 0.25;
            $transfer->next_vc_transfer = Carbon::parse($transfer->next_vc_transfer)->addMinutes(15);
            $transfer->save();
        }
    }

我的老板说有一个衬板。我在想这样的事情:

 Account::increment('balance', .25, ['next_vc_transfer' => Carbon::parse($transfer->next_vc_transfer)->addMinute(15)]).whereBetween('next_vc_transfer', [$currentMinute, $nextMinute]);

// obviously, $transfer won't work with this liner

有想法吗?

1 个答案:

答案 0 :(得分:1)

因此,这是我想出的一种衬垫:

 Account::whereBetween('next_vc_transfer_at', [$currentMinute, $nextMinute])
     ->update([
         'balance' => DB::raw('balance + 0.25'),
         'next_vc_transfer_at' => $newVCTransferAt
     ]);