Laravel insert or update multiple rows
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Inserting or Updating Multiple Rows in Laravel Tree Structures
Introduction: Adding, modifying, and managing data in complex tree structures is essential for many applications. Laravel provides useful tools to address these needs efficiently. In this blog post, we will explore the possibilities of inserting or updating multiple rows within your Laravel tree-structured database. We will cover different approaches, including techniques such as bulk updates and transactional Eloquent methods.
1. Bulk Updates: One way to perform updates on multiple rows is through mass assignment in the model. This can be done by accessing the collection attribute from your array of data.
$data = [
['id' => 1, 'name' => 'some navigation point', 'parent_id' => '0'],
['id' => 2, 'name' => 'some navigation point', 'parent_id' => '1'],
['id' => 3, 'name' => 'some navigation point', 'parent_id' => '1']
];
$collection = collect($data);
$updatedData = $collection->map(function ($row) {
return [
'navigations' => Navigation::updateOrCreate([], $row),
'result' => true
];
})->filter('result')->values();
This code block will create a new collection with updated navigation rows. The first line transforms your array into a collection, which can be easily processed later. Then, we use the updateOrCreate method to either insert or update the data depending on whether an element already exists or not. Finally, we filter the result and discard the updates that didn't modify the database.
2. Transactional Eloquent Methods: Laravel also provides transactional methods for efficient bulk operations. We can use the Transactional Closure to update all navigation rows at once.
$data = [
['id' => 1, 'name' => 'some navigation point', 'parent_id' => '0'],
['id' => 2, 'name' => 'some navigation point', 'parent_id' => '1'],
['id' => 3, 'name' => 'some navigation point', 'parent_id' => '1']
];
DB::transaction(function () use ($data) {
foreach ($data as $row) {
Navigation::updateOrCreate([], $row);
}
});
In this example, we create a Transactional Closure that encapsulates our bulk update logic. Each row is then updated using the updateOrCreate method within the loop. This ensures consistency and atomicity in our operations.
3. Conclusion: When working with Laravel tree structures, it's essential to have efficient tools for inserting or updating multiple rows at once. We have discussed bulk updates via mass assignment and transactional Eloquent methods. While there are various approaches available, each has its own pros and cons. It is up to you to decide which method best suits your project requirements and performance needs. Remember that clean code and efficient database queries will always result in better application performance.
Incorporating backlinks: https://laravelcompany.com/blog/optimizing-database-queries-in-laravel-applications