How to insert multiple rows from a single query using eloquent/fluent

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Efficiently Insert Multiple Rows from Single Query Using Eloquent/Fluent in Laravel Introduction ------------------------- As a developer working with Laravel, you might often face the need to insert multiple rows of data into a database based on the result of a single query. This task can become tricky when dealing with an unknown number of rows. In this comprehensive guide, we will introduce various methods and techniques that can help you achieve your goal efficiently using Eloquent/Fluent in Laravel. Understanding the Problem ------------------------- The problem lies in iterating through an unknown number of rows returned by a query and then inserting them into another table. We have been given the following code:
$query = UserSubject::where('user_id', Auth::id())->select('subject_id')->get();
This query returns an array of objects with both user and subject IDs, such as:
[{"user_id":8,"subject_id":9},{"user_id":8,"subject_id":2}]
Our objective is to create a new table that looks like this:
ID|user_id|subject_id
1 |8      |9
2 |8      |2
Let's examine some possible solutions. Solution 1: Using Loops with Eloquent Model Instances --------------------------------------------- This approach involves iterating through the query results and creating an instance of the model for each row using the Eloquent Model constructor. Then, you can insert the models into the database by calling the create method on the related table. Here's how it would look:
$subjectIds = [
    ["user_id" => 8, "subject_id" => 9],
    ["user_id" => 8, "subject_id" => 2]
];

foreach ($subjectIds as $item) {
    Subject::create([
        'user_id' => $item['user_id'],
        'subject_id' => $item['subject_id']
    ]);
}
Solution 2: Using Loops with Arrays of Model Data --------------------------------------------- Another method is to create an array of model data for each row and insert it into the database in bulk. This can be achieved using the Eloquent Model constructor or by creating new instances manually:
$bulkData = [];
foreach ($subjectIds as $item) {
    $bulkData[] = [
        'user_id' => $item['user_id'],
        'subject_id' => $item['subject_id']
    ];
}

Subject::insert($bulkData);
Solution 3: Using Direct SQL Queries with Laravel Fluent --------------------------------------------- You can also use Laravel Fluent for generating raw SQL queries, which allows you to perform faster operations as it avoids the overhead of Eloquent. Here's an example: DB::statement('INSERT INTO subjects (user_id, subject_id) VALUES '.implode(', ', array_map(fn($item) => '('.implode(',', [Auth::user()->id, $item['subject_id']]).')', $subjectIds))); Conclusion and Tips ------------------------- Each solution presented above can efficiently insert multiple rows from a single query into another table. If you're unsure about which approach to choose, consider the following: - Use Eloquent Model instances if you need to perform additional operations with each row (e.g., validate data or attach relationships). - Prefer using arrays of model data for bulk insertions when performance is crucial or when the number of rows is huge. - Consider direct SQL queries through Laravel Fluent for faster execution, especially in cases where Eloquent Model instances and array-based solutions might lead to multiple queries. Remember that it's always a good idea to benchmark different approaches and choose the one that best suits your specific application requirements and performance needs. Also, ensure you have proper database indexes set up to optimize the inserts. By following these guidelines and techniques, you can easily handle multiple rows from a single query using Eloquent/Fluent in Laravel projects of any size.