Reputation: 3920
My app is being made in laravel 5.6
Situation:
I have a table called "members" with a column called "membershipstatus_id".
options for status are 4, 5 and 1
4 = Active, 5 = pending and 1= expired
Target: I want to update all active(4) members to pending(5) and all pending ones to expire(1).
Solutions I have tried:
So far, below is what i have tried with no result.
// get all active and pending members
$members = Member::where('membershipstatus_id', 5)
->orWhere('membershipstatus_id', 4)
->get();
// bulk update with chunk of 200, if this is possible
$members->chunk(200, function($members)
{
foreach($members as $member)
{
// if status is pending, update to expire
if($member->membershipstatus_id == 5)
{
$member->update(['membershipstatus_id' => 1]);
}
// if status is active, update to pending, i updated a small mistake here.
if($member->membershipstatus_id == 5)
{
$member->update(['membershipstatus_id' => 4]);
}
}
}
);
return "update confirm";
Now, If anyone has a cleaner and swift way to do this, Please let me know, also, Im sure i have made some stupid mistake up there. Please point me to the right direction.
Ashish
Upvotes: 0
Views: 1463
Reputation: 18197
Use the query builder like:
// update pending to expired
DB::table('members')
->where('membershipstatus_id', 5)
->update(['membershipstatus_id' => 1]);
// update active to pending
DB::table('members')
->where('membershipstatus_id', 4)
->update(['membershipstatus_id' => 5]);
Upvotes: 1