Laravel:更新数据库的更好方法(一行)?
因此,我希望对MySQL数据库进行更新,这是我当前的更新代码:Laravel:更新数据库的更好方法(一行)?,laravel,Laravel,因此,我希望对MySQL数据库进行更新,这是我当前的更新代码: $currentMinute = Carbon::now(); $nextMinute = $currentMinute->copy()->addMinute(); $transfersWithinRange = Account::whereBetween('next_vc_transfer', [$currentMinute, $nextMinute])->get(); if
$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
想法?所以,这是我为一行提出的:
Account::whereBetween('next_vc_transfer_at', [$currentMinute, $nextMinute])
->update([
'balance' => DB::raw('balance + 0.25'),
'next_vc_transfer_at' => $newVCTransferAt
]);
我想我在你的代码上看到了一个拼写错误:Account::increment('balance',.25,['next\u vc\u transfer'=>Carbon::parse($transfer->next\u vc\u transfer)->addMinute(15)]->whereBetween('next\u vc\u transfer',[$currentMinute$nextMinute]);将“.”改为“->”谢谢,我来自JavaScript思维。