Skip to main content

What is the Eloquent ORM?

Laravel includes an object-relational mapper (ORM) called Eloquent. Eloquent makes it easy to interact with the database and implements the Active Record pattern. With Eloquent, you create a “model” class that corresponds to each table in the database. You retrieve, insert, update, and delete records through the model.
Before using Eloquent, configure your database connection in config/database.php. By default, the DB_* settings in your .env file are used.

Creating a model

Generate a new model with the make:model Artisan command.
To create a migration at the same time, use the -m option.
The model is created in the app/Models directory.

Model-to-table mapping

Eloquent infers the table name from the class name automatically. The class name converted to a snake_case plural becomes the table name. If your table name does not follow the naming convention, define the $table property on the model to specify it explicitly.

Timestamps

By default, Eloquent automatically manages the created_at and updated_at columns. Include $table->timestamps() in your migration and Eloquent will set these values for you when the model is saved or updated. To disable automatic timestamp management, set $timestamps to false.

Mass assignment protection

When saving data in bulk with Eloquent, you must configure mass assignment protection.

fillable

Use the $fillable property to specify which columns may be mass assigned.

guarded

Conversely, use $guarded to specify which columns cannot be mass assigned.
Setting $guarded to an empty array allows mass assignment on every column. When you pass user input directly, there is a risk that unintended columns will be overwritten, so we recommend explicitly listing allowed columns with $fillable.

Eloquent query execution flow

Here is how a query such as User::where()->get() is executed internally.

Basic CRUD operations

Retrieving records (Read)

Retrieve every record:
Retrieve records with a condition:

Creating records (Create)

Use the create method to insert a single record (you must configure $fillable):
You can also create an instance and assign values individually.

Updating records (Update)

Retrieve the model, change its values, and call save to persist the update.
Use update to update multiple columns at once.
You can also update every record that matches a condition in one call.

Deleting records (Delete)

Use the delete method to remove a record.
You can also delete directly by ID.

Common query methods

Practical example: working with the Post model

Here is a controller that operates on the posts table created by a migration.
When you type-hint a controller method parameter as Post $post, Laravel automatically retrieves the model from the route parameter (route model binding). You no longer need to call Post::findOrFail($id) yourself.

The model event lifecycle

Here are the events fired when you call save(). Different events fire depending on whether it is a create or an update, but saving and saved fire in both cases.

Next steps

Migrations

A refresher on creating the tables Eloquent uses with migrations.
Last modified on August 2, 2026