Pankaj
Pankaj

Reputation: 10115

Trying to bulk update instead of one by one in Laravel 5.2.31

I am using the following code to check all active memberships of the user

$UserMemberships = \App\Models\User\Membership\UserMembershipModel
::where('UserID', $UserID)
->where('IsActive', true)
->get();

Then I am setting the active status of membership to false one by one.

foreach($UserMemberships as $UserMembership) {
    $UserMembership->IsActive = false;
    $UserMembership->save();
}

Is there any way to do it in one shot?

Upvotes: 0

Views: 42

Answers (1)

Raghav Patel
Raghav Patel

Reputation: 843

You can directly update record, try this query:

$UserMemberships = \App\Models\User\Membership\UserMembershipModel
::where('UserID', $UserID)
->where('IsActive', true)
->update(['IsActive' => false]);

Upvotes: 1

Related Questions