Skip to main content

What is Eloquent?

Laravel includes Eloquent, an object-relational mapper (ORM) that makes database interaction straightforward. Eloquent implements the Active Record pattern. With Eloquent, each database table has a corresponding “model” class. You use the model to retrieve, insert, update, and delete records in that table.
Before using Eloquent, configure your database connection in config/database.php. By default, Laravel reads the DB_* values from your .env file.

Creating a model

Use the make:model Artisan command to generate a new model:
To create a model together with its migration file, add the -m flag:
Models are created in the app/Models directory:

Table naming conventions

Eloquent automatically infers the table name from the class name — it converts the class name to snake_case and pluralizes it: If your table name does not follow this convention, define a $table property on the model:

Timestamps

Eloquent automatically manages created_at and updated_at columns. If you add $table->timestamps() in your migration, Eloquent sets the values automatically when you save or update a model. To disable automatic timestamp management:

Mass assignment protection

When inserting records in bulk, you need to configure mass assignment protection.

fillable

Specify which columns are mass-assignable using the $fillable property:

guarded

Alternatively, use $guarded to specify which columns should not be mass-assignable:
Setting $guarded to an empty array allows mass assignment for every column. If you pass user-supplied data directly, unintended columns could be overwritten. Prefer $fillable to explicitly allow only the columns you expect.

Basic CRUD operations

Reading records

Retrieve all records:
Retrieve records with conditions:

Creating records

Use the create method to insert a single record (requires $fillable to be configured):
You can also instantiate a model and assign properties individually:

Updating records

Retrieve the model, change its properties, then call save:
Use the update method to update multiple columns at once:
Update multiple matching records in a single query:

Deleting records

Call delete on a retrieved model:
Delete by primary key directly:

Common query methods

Practical example: a PostController

Here is a controller that uses the Post model to handle typical blog post operations:
When a controller method type-hints a model like Post $post, Laravel automatically fetches the matching record from the database based on the route parameter. This is called route model binding, and it removes the need to write Post::findOrFail($id) yourself.

Next steps

Database migrations

Review how to create the tables that Eloquent models map to.
Last modified on March 29, 2026