Eloquent model mass update

前端 未结 7 1661
长发绾君心
长发绾君心 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 00:56

    laravel 5.8 you can accomplish mass update like so:

    User::where('id', 24)->update (dataAssociativeArray) ;
    
    0 讨论(0)
  • 2020-12-03 01:01

    If you need to update all data without any condition, try below code

    Model::query()->update(['column1' => 0, 'column2' => 'New']);
    
    0 讨论(0)
  • 2020-12-03 01:05

    Perhaps this was not possible a few years ago but in recent versions of Laravel you can definitely do:

    User::where('age', '<', 18)->update(['under_18' => 1]);
    

    Worth noting that you need the where method before calling update.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-03 01:20

    Use database transactions to update multiple entities in a bulk. Transaction will be committed when your update function finished, or rolled back if exception occurred somewhere in between.

    https://laravel.com/docs/5.4/database#database-transactions

    For example, this is how I regenerate materialized path slugs (https://communities.bmc.com/docs/DOC-9902) for articles in a single bulk update:

    public function regenerateDescendantsSlugs(Model $parent, $old_parent_slug)
        {
            $children = $parent->where('full_slug', 'like', "%/$old_parent_slug/%")->get();
    
            \DB::transaction(function () use ($children, $parent, $old_parent_slug) {
                /** @var Model $child */
                foreach ($children as $child) {
                    $new_full_slug  = $this->regenerateSlug($parent, $child);
                    $new_full_title = $this->regenerateTitle($parent, $child);
    
                    \DB::table($parent->getTable())
                        ->where('full_slug', '=', $child->full_slug)
                        ->update([
                            'full_slug' => $new_full_slug,
                            'full_title' => $new_full_title,
                        ]);
                }
            });
        }
    
    0 讨论(0)
  • 2020-12-03 01:20

    Laravel 6.*

    We can update mass data on query as follow :

    Appointment::where('request_id' , $appointment_request->id)->where('user_id', Auth::user()->id)->where('status', '!=', 'Canceled')->where('id', '!=', $appointment->id)->update(['status' => 'Canceled', 'canceled_by' => Auth::user()->id]);
    
    0 讨论(0)
提交回复
热议问题