How to insert multiple records with createMany method in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Mass Creation: How to Insert Multiple Records with `createMany` in Laravel
As developers working with dynamic forms and relational data in Laravel, one of the most common challenges is efficiently inserting multiple related records in a single database operation. When dealing with repeatable form fields—like creating multiple options for a single question—the `createMany` method in Eloquent becomes your best friend. However, structuring the input data correctly to avoid complex nesting and data replication issues requires a solid understanding of how Laravel handles mass assignment.
This post will walk you through the correct, robust way to use `createMany` when inserting multiple records based on array input from a repeatable form, ensuring data integrity and efficiency.
## Understanding the Challenge with Repeatable Data
You are facing a scenario where a single request contains an array of items (e.g., multiple titles, descriptions, and IDs for options). When you try to pass this complex structure directly to `createMany`, you often end up with deeply nested arrays that confuse Eloquent's mass assignment mechanism, leading to unexpected data structures rather than clean database insertions.
The goal is not just to insert the data, but to insert *many* records related to a parent model efficiently.
## The Correct Approach: Structuring Data for `createMany`
The key to using `createMany` effectively is ensuring that the array you pass it is a flat collection of models or an array of attribute arrays, where each element corresponds directly to a row you wish to insert. You should avoid building complex, multi-level arrays *before* calling the method if possible; instead, structure your input data to mirror the required model attributes.
Let's assume you have a `Question` model and a related `Option` model that has a relationship defined. We want to create multiple `Option` records for a given `Question`.
### Step-by-Step Implementation Example
In this example, we will focus on handling an array of options submitted via a form and inserting them into the database in one go.
First, ensure your Eloquent relationships are set up correctly. For instance, if you have a `Question` model that has many `Option`s:
```php
// app/Models/Question.php
public function options()
{
return $this->hasMany(Option::class);
}
```
Now, let's look at how to process the request and use `createMany`.
```php