更新所有行

时间:2017-08-27 22:40:09

标签: php mysql laravel laravel-5.2

public function postWorkerActivityPlus()
{
    $current = Carbon::now();
    $WorkerActivity = Model::where('at_work',1)->where('updated_at','<',$current)->get();

    $d = $a / $b * $c ;

    foreach ($WorkerActivity as $key => $wa) {
        $updateActivity = Model::find($wa->id);
        if ($wa->worker_activity <= $a) {
            $updateActivity->worker_activity += $d;
            $updateActivity->save();
            return redirect()->back()->with('info', 'You updated worker');

        }
    }
}

当第一个worker_activity达到$ a时,此结果首先更新$ updateActivity,进入第二个等等。我的任务一次全部更新

1 个答案:

答案 0 :(得分:2)

更改返回位置

public function postWorkerActivityPlus()
{
$current = Carbon::now();
$WorkerActivity = Model::where('at_work',1)->where('updated_at','<',$current)->get();

$d = $a / $b * $c ;

foreach ($WorkerActivity as $key => $wa) {
$updateActivity = Model::find($wa->id);
if ($wa->worker_activity <= $a) {
$updateActivity->worker_activity += $d;
$updateActivity->save();

}
return redirect()->back()->with('info', 'You updated worker');
}