Models in Laravel represent the data layer of an application. They interact with the database and contain the business logic related to data. Laravel uses Eloquent ORM (Object–Relational Mapping), which allows developers to work with database records as PHP objects in a simple and expressive way.
A model represents a database table and is responsible for:
Retrieving data
Inserting new records
Updating existing records
Deleting records
Models are stored in:
Example:
A User model typically represents the users table.
Laravel provides an Artisan command to create models:
This creates:
To create a model with a migration:
Example model:
By convention:
Model name → Singular (Post)
Table name → Plural (posts)
To protect against unwanted data insertion, Laravel uses mass assignment rules.
Or:
Or using mass assignment:
Or:
Eloquent makes database relationships easy.
Chained queries:
By default, Eloquent manages timestamps:
created_at
updated_at
To disable:
Models and Eloquent ORM provide a powerful and intuitive way to interact with databases in Laravel. By using expressive syntax and built-in relationships, Eloquent simplifies database operations while maintaining clean, readable, and maintainable code.
Take quizzes related to this topic and see where you stand!
Start Quiz Now