Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FutureWeb's avatar

Update multiple records using Eloquent

Hi LarPeeps,

How can I update multiple rows with a single query like:

$update = mysql_query("update item_table set colour = 'black' where item_type_id = 1");

lets say I have 20 rows with the item_type_id of 1 the above would update them all, but when I try:

$update = ItemTable::where('item_type_id', '=', 1);
    $update->colour = 'black';
    $update->save();

laravel spits its dummy out :( any help would be greatly appreciated.

Thanks in advance.

0 likes
10 replies
warksit's avatar
warksit
Best Answer
Level 6
ItemTable::where('item_type_id', '=', 1)
->update(['colour' => 'black']);
30 likes
pmall's avatar
ItemTable::where('item_type_id', '=', 1)->update(['color' => 'black']);
4 likes
iftekhar's avatar

Hi guys, These too works..

$values=array('column1'=>'value','column2'=>'value2');
ItemTable::whereIn('primary_key','value')->update($values);

Note: whereIn() is used for arrays..

7 likes
renz18x's avatar

What if you want to update multiple columns in a row?

sunergetic's avatar

@renz18x to update multiple columns

ItemTable::where('item_type_id', '=', 1)
    ->update([
        'colour' => 'black',
        'size' => 'XL', 
        'price' => 10000 // Add as many as you need
    ]);

or with whereIn()

$itemTypes = [1, 2, 3, 4, 5];

ItemTable::whereIn('item_type_id', $itemTypes)
    ->update([
        'colour' => 'black',
        'size' => 'XL', 
        'price' => 10000 // Add as many as you need
    ]);

6 likes
laurence702's avatar

I know this thread was created 3yrs ago. But Im trying to update multiple rows when a user sends an array of id. eg user send me array of applicant id's [3,4,5,6] then I update a boolean column called "isStarred" to 1 for all those applicants. Thanks in advance

chuck_wood's avatar

Bear in mind that these are "bulk" methods and so won't fire model events!

2 likes
79man's avatar

I think this should do the trick:

User::whereIn('id', $applicant_ids)
    ->update([
        'IsStarred' =>  1,
    ]);
maizied's avatar

Update Specific column you want to upadate using Laravel eloquent.

Way -> 1

[Status-> Column you want to update]

Model::where('status', '=', 1)->update(['status' => 0]);

$updateStatus = new Model();

$updateStatus->status = $request->input('status');

$updateStatus->save();

Way -> 2

[Status-> Column you want to update]

$allData = Model::where('status', 1)->get();

            foreach ($allData as $data){
                $data->status = 0;

                $data->update();

			}

$updateStatus = new Model();

$updateStatus->status = $request->input('status');

$updateStatus->save();

Sorry for the convenience for not using codeblocks

Please or to participate in this conversation.