Mohamed Allam
Laravel Example

Laravel Example

Laravel Eloquent Create or Insert?

Laravel Eloquent Create or Insert?

When you leverage Laravel and Eloquent ORM, many times storing data can be tricky. let's compare insert() and create() methods.

Mohamed Allam's photo
Mohamed Allam
·Oct 27, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

In all our apps, at some point or another, we need to save a record to the database, luckily Laravel helps us tremendously using Eloquent.

Laravel's documetation introduction to Eloquent:

Laravel includes Eloquent, an object-relational mapper (ORM) that makes it enjoyable to interact with your database. When using Eloquent, each database table has a corresponding "Model" that is used to interact with that table. In addition to retrieving records from the database table, Eloquent models allow you to insert, update, and delete records from the table as well.

If you are coming from Java world and Hibernate, this is Hibernate of PHP and Laravel.

If you are coming from NodeJS world and Sequelize.

Entity Framework and C#, you got the basic idea.

Eloquent has some elegant awesome features to do all CRUD operations on our databases.

Arguably its the best piece of the entire Laravel Framework. Now whenever we are about to store data to the database. we can choose from a veraity of methods, helping our process. but thats where we might be curious as using wrong methods would affect our app or its behaviour if we are not cautious enough.

The question

Given you have some ModelName, you want to insert data to the database, there is multiple ways but two popular elegant and easy to guess methods are. insert or create. ModelName::insert([ 'field' ⇒ 'value' ]);

ModelName::create([ 'field' ⇒ 'value' ]);

This is tricky, are they identical? no. Definitly no.

I used Spatie Ray. to dig out query the events

The solution

The create() method would pass thourgh many layers and will hit insert() at some point which calls the QueryBuilder, to persist data to our database.

The insert method would not care about mass asignement, nor it would broadcast all model events. this operation would only broadcast booting and booted Eloquent models events. then excute the query. However the create is going to go through more layers like mass assignement, and trigger saving, creating, created and saved, eloquent events consecutively. after the boot ones. This will broadcast 4 extra events of our application. and overall takes more time going trough more layers. One of them is the getters and setters, also known as mutators and accessors. this wont work if you use insert(). so this can significanly change the behaviour of your persisting of data.

If you ask me, it depends on how you want to use Eloquent, I recomand using Laravel conventions. create which has more features. and will make your persistance of data. centralized. thats for average to beginner level users.

If you are an expert you wont be questioning this much, and whatever you do is perfect.

Tip: One of useful case of insert, is when you use Repository pattern. If you intend to use the Repository class to do differently than models. persistance retrival operations. model behaviours, like casting data.

Share this