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

Ligonsker's avatar

How to mass update in Laravel 6?

Hello,

I have an array that contains arrays that represent rows to update, where the key is the id in the table:

[
  22   => ['col1' => 'data', 'col2' => 'data'],
  31    => ['col1' => 'data', 'col2' => 'data']
  352 => ['col1' => 'data', 'col2' => 'data']
]

I need to update the values where the row matches the given id key.

How can I mass update in Laravel 6? Will I need to use a loop?

thanks

0 likes
1 reply
LaryAI's avatar
Level 58

You can use the update() method on the model to mass update records in Laravel 6. Here's an example of how you can use it to update the rows in your array:

$data = [
    22 => ['col1' => 'data', 'col2' => 'data'],
    31 => ['col1' => 'data', 'col2' => 'data'],
    352 => ['col1' => 'data', 'col2' => 'data']
];

// Get the model for the table you want to update
$model = new YourModel();

foreach ($data as $id => $values) {
    // Use the update() method to update the row with the given ID
    $model->where('id', $id)->update($values);
}

This code loops through the rows in your array and uses the update() method to update each row with the given ID. The where() method is used to specify the ID to update, and the $values array is passed to the update() method to set the new values for the row.

Please or to participate in this conversation.