Eloquent model mass update

前端 未结 7 1663
长发绾君心
长发绾君心 2020-12-03 00:41

Please correct me if I am wrong, but I think there is no such thing as mass update in an Eloquent model.

Is there a way to make a mass update on the DB table without

7条回答
  •  独厮守ぢ
    2020-12-03 01:20

    A litle correction to @metamaker answer:

    DB::beginTransaction();
         // do all your updates here
            foreach ($users as $user) {
                $new_value = rand(1,10) // use your own criteria
                DB::table('users')
                   ->where('id', '=', $user->id)
                   ->update(['status' => $new_value  // update your field(s) here
                    ]);
            }
        // when done commit
    DB::commit();
    

    Now you can have 1 milion different updates in one DB transaction

提交回复
热议问题