Defining Models and Migrations
Every Eloquent model needs a matching database table to represent, and Laravel uses migrations to define that structure in code. Together, models and migrations form the backbone of how a Laravel application manages its data layer.
What Is a Model?
A model is a PHP class, usually generated through Artisan, that represents a single database table. By convention, Laravel assumes a model named Post corresponds to a table named posts, though this can be overridden when needed.
What Is a Migration?
A migration is a version-controlled PHP file that describes changes to the database schema, such as creating a table or adding a column. Because migrations live alongside application code, the entire team can stay in sync on the database structure without manually running SQL scripts.
| File Type | Purpose |
|---|---|
| Model | Represents and interacts with a database table in PHP |
| Migration | Defines or modifies the actual table structure |
| Factory | Generates fake data for testing based on a model |
| Seeder | Populates the database with initial or sample records |
Generating Both Together
- Artisan can generate a model and its migration in a single command
- Column definitions live inside the migration's
up()method - Model properties like
$fillablecontrol which fields can be mass-assigned
With a model and its matching table structure in place, the model is finally ready to actually store and retrieve data — starting with the basic CRUD operations covered next.
Ready to master Laravel Training Course?
Join Uncodemy's hands-on Laravel program and build real, production-ready applications.
