How to update multiple rows of a table in laravel?

Member

by jasen , in category: PHP Frameworks , 6 months ago

How to update multiple rows of a table in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by shyann , 6 months ago

@jasen 

To update multiple rows of a table in Laravel, you can use the update() method on the query builder to update records that match a specific condition. Here is an example of how you can update multiple rows in a table:

1
2
3
$affectedRows = DB::table('users')
              ->where('status', 'active')
              ->update(['status' => 'inactive']);


In this example, we are updating all rows in the users table where the status column is equal to 'active' and setting their status column to 'inactive'.


You can add additional conditions to target specific rows for updating. Make sure to add any necessary conditions to avoid unintentionally updating rows that you do not want to change.