Differences Between insert, create, and createMany in Laravel: When and How to Use

Explore the differences between Laravel’s insert, create, and createMany for efficient and secure data handling.

Adrian Generous
3 min readFeb 9, 2024

--

Laravel offers a variety of methods for manipulating database data, among which insert, create, and createMany are commonly used for adding new records. Each of these methods serves a similar purpose but differs in application, behavior, and requirements, which is crucial for the efficiency and security of the application.

What is insert?

The insert method is a low-level approach to directly adding records to the database, allowing one or multiple records to be inserted simultaneously without creating an instance of the Eloquent model.

Example:

DB::table('users')->insert([
['email' => 'jan@example.com', 'name' => 'Jan'],
['email' => 'anna@example.com', 'name' => 'Anna']
]);

When to use? Use insert when you want to quickly add many records without additional model logic and do not need data validation at the application level before saving.

When not to use? Do not use insert when the record addition operation should be covered by Eloquent model validation, as insert does not use model validation logic and does not trigger model events.

Additional tips: When adding records in bulk, remember the database limitations on the maximum number of records that can be added in a single query.

What is create?

The create method uses the Eloquent model to add new records, applying mass assignment and requiring the definition of attributes that can be mass-assigned in the model.

Example:

User::create([
'email' => 'jacek@example.com',
'name' => 'Jacek'
]);

When to use? Use create when you want to utilize validation, events, and mutators of the Eloquent model and ensure greater security by limiting the attributes that can be mass-assigned.

When not to use? Do not use create when you want to add multiple records simultaneously and are concerned with optimizing performance, as create adds records one at a time.

Additional tips: Always define fillable or guarded in your models to prevent unauthorized mass assignment of data.

What is createMany?

The createMany method is specially designed for working with Eloquent model relationships, allowing multiple related records to be added at once.

Example:

$user = User::find(1);
$user->posts()->createMany([
['title' => 'Post 1', 'content' => 'Content of post 1'],
['title' => 'Post 2', 'content' => 'Content of post 2']
]);

When to use? createMany is ideal when working with Eloquent model relationships and you want to add multiple related records at once, managing relationships and foreign keys automatically.

When not to use? Do not use createMany if you are not working with Eloquent model relationships or when you want to add a single record. It is a method specific to adding multiple related records.

Sample SQL Queries

  • Insert
    INSERT INTO users (email, name) VALUES ('jan@example.com', 'Jan'), ('anna@example.com', 'Anna');
  • Create
    INSERT INTO users (email, name, created_at, updated_at) VALUES ('jacek@example.com', 'Jacek', '2021-01-01 00:00:00', '2021-01-01 00:00:00');
  • CreateMany
    INSERT INTO posts (user_id, title, content, created_at, updated_at) VALUES (1, 'Post 1', 'Content of post 1', '2021-01-01 00:00:00', '2021-01-01 00:00:00'), (1, 'Post 2', 'Content of post 2', '2021-01-01 00:00:00', '2021-01-01 00:00:00');

Performance Analysis

insert is typically the fastest way to add many records, whereas create and createMany perform additional logic, which can affect the execution time of operations, especially when adding many records.

Summary

Choosing between insert, create, and createMany in Laravel depends on the specifics of the use case, security, validation, and performance requirements. Each of these methods has its place in the Laravel developer's arsenal, offering different levels of control and flexibility when working with data.

--

--

Adrian Generous

I navigate between coding & marketing, experienced as a CTO, project manager, & ad agency owner. I share Laravel & more on Medium, also a history enthusiast.