Insert Multiple Records At Once With Laravel
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Insert Multiple Records Using Eloquent Models in Laravel
Introduction
In software development projects, it is quite common to work with massive volumes of data. For instance, you might have a list of books and need to add them to your system all at once. In this blog, we will explore the best practices for inserting multiple records simultaneously using Eloquent models in Laravel.
What Are Eloquent Models?
Eloquent is an ORM or Object Relational Mapper that simplifies working with databases in Laravel apps. It automatically converts database tables into classes called Eloquent models, making the data access and manipulation easier. These classes allow developers to interact with the database using an intuitive object-oriented approach.
Inserting Multiple Records
When inserting multiple records one by one (sequential inserts), it can be time-consuming if there are many records to add. To optimize this process, we can use Eloquent models and mass assignment for efficient batch insertion. Here's a sample code that demonstrates how to do it:
public function add(Request $request)
{
if ($request->ajax()) {
$books = $request->books;
foreach ($books as $book) {
if (!empty($book)) {
// Create a new Book model instance with the book name and current authenticated user's ID
$add = new Book([
'name' => $book,
'user_id' => Auth::user()->id
]);
// Save the Book model instance in one go
$add->save();
}
}
}
}
In this example, we are using a JSON array (books) containing all book names to insert. We iterate through the array and create a new Book model instance for each record. The model has two properties: 'name' and 'user_id'. The first one is populated with the current book name, while the second one assigns the authenticated user's ID (assuming you want to track which books belong to which users). Finally, we use the save() method to insert all records in a batch.
Optimizations and Best Practices
To ensure maximum performance when using Eloquent models for mass inserts, consider these best practices:
- Use transactions to save multiple related records at once: Transactions help guarantee atomic actions, ensuring that either everything is committed or nothing happens.
- Optimize database table indexes: Make sure your tables are properly indexed and that you create unique constraints where necessary. This will speed up the process and improve performance.
- Employ queues for background processing: When working with large datasets, it may be more efficient to perform batch inserts asynchronously using Laravel's queue functionality.
- Monitor database connections: Ensure your app has sufficient database connections to handle peak loads without affecting performance.
Conclusion
Inserting multiple records at once is an important skill for any developer working with a data-driven application. Using Eloquent models and the save() method, you can efficiently batch insert records into your database. Remember to optimize your database tables, use transactions when necessary, and leverage queues for asynchronous processing if dealing with large datasets. With these tips in mind, you'll be well on your way to improving your Laravel data handling skills and creating more efficient applications.