How to Bulk Insert Data with Eloquent in Laravel

Updated: January 22, 2024 By: Guest Contributor Post a comment

Introduction

Bulk inserting data in a database is a common task for developers, especially when dealing with large amounts of data. Laravel, a popular PHP framework, provides an easy and efficient way to handle bulk inserts using Eloquent ORM. In this tutorial, we will discuss how to use Eloquent to perform bulk inserts, which can improve the performance of your application by minimizing the number of queries to the database.

Prerequisites

  • Basic understanding of PHP and Laravel
  • Laravel installation on your development machine
  • A database configured in your Laravel application

Understanding Eloquent

Eloquent is an ORM (Object-Relational Mapping) included with Laravel, which simplifies interactions with the database by mapping database tables to classes. With Eloquent, we can easily insert, update, and delete records in our database while writing clean and readable code.

Single Insert vs. Bulk Insert

Before diving into bulk inserts, it’s essential to understand the difference between single and bulk inserts:

  • Single Insert: Adds one record to the database at a time. This is what you normally do using Model::create() or $model->save().
  • Bulk Insert: Adds multiple records to the database in a single query, which is much faster for inserting large datasets.

Preparing Your Data

First, make sure that your data is in an array where each element of the array represents a record you want to insert. It should look something like this:

$users = [
    ['name' => 'Alice', 'email' => '[email protected]', 'password' => bcrypt('password')],
    ['name' => 'Bob', 'email' => '[email protected]', 'password' => bcrypt('password')],
    // ... more users
];

Using the insert Method

The easiest way to perform a bulk insert in Eloquent is by using the insert method provided by the query builder. Here’s how to do it:

use App\Models\User;

User::insert($users);

This will create a single SQL query that inserts all the users from the array into your database.

Handling Large Datasets

If you have a very large dataset, you may run into memory issues. In such cases, it’s better to chunk your dataset and insert it in batches, like so:

$usersChunks = array_chunk($users, 500);

foreach ($usersChunks as $chunk) {
    User::insert($chunk);
}

This code will split the $users array into chunks of 500 records and insert each chunk into the database one at a time.

Timestamps and Bulk Inserts

When using the insert method, remember that it won’t automatically handle timestamps for the created_at and updated_at columns. You’ll need to manually add these if required:

$time = now();
$users = array_map(function ($user) use ($time) {
    return $user + ['created_at' => $time, 'updated_at' => $time];
}, $users);

User::insert($users);

Using Eloquent’s create Method with array_map

Though not as efficient as insert, you can also use Eloquent’s create method combined with array_map function for a more object-oriented approach:

array_map(function ($user) {
    User::create($user);
}, $users);

Remember that this approach will generate individual queries for each record, so it’s not recommended for very large data sets.

Conclusion

In this tutorial, we have explored how to effectively use Eloquent to perform bulk inserts in Laravel. This can be particularly useful when dealing with large amounts of data that need to be inserted into the database efficiently. By following the best practices and examples provided, you should now be able to implement bulk inserts in your Laravel applications with ease.

It is essential to test your bulk insert operations thoroughly, especially when working with large datasets, to ensure data integrity and optimize performance.