Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shanaka111's avatar

The Difference between a model and a migration

So whts the difference between a model and a migration???

0 likes
7 replies
zachleigh's avatar

A model is a class representation of a database column. A migration is the database table schema, it simply describes the database columns. Completely different.

2 likes
baumgars's avatar

@zachleigh A model doesnt just represent a single database column. It represents all fields within a table and like stated later in this thread contains all the methods that represent relationships and queries on a table. A model is the business logic for a table and the migration is the db schema of the table. If you run a migration, you "migrate" or in another word, you create a table in your database based on the schema in your migration file.

ashutoshme's avatar

Also in addition, Model is used to handle the data you are getting from database, so that you can apply different types of functions and features of database easily. As you just have to define the relationship, then when you are fetching data using Model, you can just used that relationship function, and all related data will be fetched, and you don't have to handle manual query.

Mittensoff's avatar

Migrations are files that have commands for changing your database (creating tables, changing tables using alter, dropping tables etc).

So basically you use migrations to have an overview of what's done to your database after you do php artisan migrate. Imagine if you had 10 people in your team all making changes to your database, it would be chaos. Like this, you know exactly what's been done.

Models are there to be a "wrapper" for your database. It contains functions so you don't have to do complex/long SQL queries to do something that models do in just a $user->posts(). That's one example, but think of Models as 'improved database' (at least in terms of ease of use).

3 likes
martinbean's avatar

@Tray2 Hopefully OP did make a start on learning Laravel in the 8 years since they posted this thread πŸ˜…

Tray2's avatar

@martinbean Odd that it showed up on top in the forum. Didn't even check the date on it.

Please or to participate in this conversation.