laravel 5.6 bulk inserting json data
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Bulk Insertion in Laravel: Efficiently Handling Large JSON Datasets
As a senior developer working with Laravel APIs, optimizing database operations is paramount for scalability and performance. When dealing with tasks like inserting 40 records from a frontend request, relying on individual Eloquent `save()` calls inside a loop is an anti-pattern. It introduces significant overhead due to repeated database connection setup, transaction management, and query parsing for every single record.
The core question here is: How do we perform a bulk insert without resorting to complex multi-dimensional arrays in the application layer? The answer lies in leveraging native database capabilities, specifically using Laravel’s optimized methods for mass data insertion.
This guide will explore the most effective strategies for bulk inserting JSON data into your MySQL database efficiently in a Laravel environment.
## Why Bulk Insertion Matters
When you receive 40 separate objects from your frontend, and process them one by one:
```php
// Inefficient approach (Avoid this for large batches)
foreach ($data as $item) {
$model = new ExamPaper();
$model->fill($item);
$model->save(); // Executes 40 separate INSERT queries
}
```
This results in 40 separate round trips to the MySQL server. This is slow, puts unnecessary strain on your application server, and severely limits the throughput of your API. For high-volume data entry, we must consolidate these operations into a single, atomic database command.
## Method 1: Using Eloquent's `insert()` for Collection Bulk Operations
Laravel provides a convenient way to handle bulk inserts using the static `insert()` method on a model. This method is often cleaner and more idiomatic than raw SQL if you are dealing with simple insertions where you don't need complex relationships immediately established.
First, ensure your database table structure matches the data you intend to insert. Let’s assume you have an `exam_papers` table.
### Step 1: Prepare the Data Array
You need to transform your incoming JSON array into a flat array of arrays, where each inner array represents one row ready for insertion.
```php
// Sample structure we want to insert (flattened)
$dataToInsert = [
[
'paper_id' => '5',
'question_no' => '2',
'question' => 'test insert code',
'answer1' => 'answer1',
// ... other fields mapped to the DB columns
],
[
'paper_id' => '5',
'question_no' => '3',
'question' => 'test insert code',
'answer1' => 'answer1',
// ...
]
// ... up to 40 records
];
```
### Step 2: Execute the Bulk Insert
You can use the Eloquent `insert()` method, passing the collection of data. This is highly efficient because it executes a single SQL `INSERT` statement for the entire batch.
```php
use App\Models\ExamPaper;
// $dataToInsert holds your array of 40 records
$insertedRecords = ExamPaper::insert($dataToInsert);
return response()->json(['message' => 'Data successfully bulk inserted.', 'count' => $insertedRecords]);
```
This approach is excellent for simple insertions and keeps the code tightly integrated with the Laravel ORM, which aligns perfectly with best practices documented on the [Laravel company website](https://laravelcompany.com).
## Method 2: The High-Performance Route – Raw SQL `insert()`
For maximum performance, especially when dealing with hundreds or thousands of records, executing a raw SQL `INSERT INTO ... VALUES (...), (...), (...)` statement is often superior to Eloquent's collection method. This technique minimizes the overhead of PHP/Laravel object hydration during the insertion phase by letting the database handle the entire batch operation in one go.
### Implementation Example
If you are inserting data into an `exam_papers` table with columns: `paper_id`, `question_no`, `question`, `answer1`, etc., your raw query would look like this:
```php
use Illuminate\Support\Facades\DB;
// $dataToInsert is the flattened array of 40 records from the frontend
$tableName = 'exam_papers';
$columns = [
'paper_id', 'question_no', 'question', 'answer1', 'answer2', 'answer3', 'answer4', 'answerC', 'knowarea'
];
// Prepare values for insertion (requires careful mapping of the input array)
$values = [];
foreach ($dataToInsert as $row) {
// Ensure the order matches the columns defined above!
$values[] = [
$row['paper_id'],
$row['question_no'],
$row['question'],
$row['answer1'],
$row['answer2'],
$row['answer3'],
$row['answer4'],
$row['answerC'],
$row['knowarea']
];
}
// Construct the bulk insert query
if (!empty($values)) {
DB::table($tableName)->insert($values);
}
return response()->json(['message' => 'Bulk data inserted via raw SQL.']);
```
## Conclusion: Choosing the Right Tool
For your specific scenario—bulk inserting structured JSON data—**Method 2 (Raw SQL `insert()` on a constructed multi-row statement)** offers the absolute best performance. It bypasses the overhead of Eloquent model hydration for every row, making it the fastest way to populate your tables when dealing with large batches.
However, if you prefer the abstraction and maintainability of the ORM, **Method 1 (Eloquent `insert()` on a Collection)** is perfectly adequate, especially if the volume remains moderate. Always profile your application, but for bulk operations, optimizing at the database level is key to building robust APIs like those found on [Laravel company website](https://laravelcompany.com).