Home > Back-end >  Laravel. Difference between php artisan make:model and make:migration
Laravel. Difference between php artisan make:model and make:migration

Time:08-02

I am new to Laravel and learning myself. I have confusion between Laravel commands "php artisan make:model" and "php artisan make:migration". What I understand is that both of these commands are used for creating table.

CodePudding user response:

php artisan make:model is used to create a Model-Class, referring to the MVC Pattern.

php artisan make:migration is used to create a Migration-Class. A Migration is used to define a database-table schema. The Migrations will be executed when you run php artisan migrate, which will create the tables in your database of choice.

You can even combine these two tasks:

php artisan make:model Flight --migration

Will create a Model-Class called Flight and a migration with the filename 2022_08_01_162322_create_flights_table.

CodePudding user response:

Those are two different commands.

php artisan make:model

This command will generate a Model. For exemple, you could generate a Listitem model if you build a to-do app.

php artisan make:migration

This command will generate a migration file. You will use this file to create your database table structure.

In everyday life, I prefer using the first command like this:

php artisan make:model -mc

The -mc option will ask Laravel to create a migration file and a controller file in addition to the model file. I recommend using this command to save time.

CodePudding user response:

You need to first know the difference between Model and Migration:

Model: represents the structure of an entity in your database schema. That means that this migration when executed php artisan migrate will represent and create the table with that defined structure
Migration: represents the domain logic. this logic is used to handle the data passed between the database and the user interface (View in MVC).

So, php artisan make:model Example will create a new model of Example and php artisan make:migration example_table will create a migration that refers a model.

CodePudding user response:

First Do You Know What's Model And Migration

Model:Is Refer To Your Table Inside Your Database But If You Want To Refer to your table you should Make the name of the model like your Table name in database but : 1- The first letter is cabital 2-Remove 's' from the last You Can Craete model using php artisan make:model YourModelName

Migration:is used to make rows inside your table like name,email,password

You Can Craete Migration using php artisan make:migration YourMigrationName

CodePudding user response:

To be more clear in a simple word :

1️⃣ Model is a logic which transfers data between database and UI.

2️⃣ Database is is a collection of organised data and information. These databases contain many tables with multiple fields containing information on a company's clients or employees.

3️⃣ Migration is a logic which you can make your tables and columns programmatically and also you can have a type of version control on your database tables.

➡️ So : While running

php artisan make:model

You have created a file to write logic to transfer data between database and UI

While running

php artisan make:migration

You have created a file which you can defining your table fields.

And after that you should run :

pho artisan migrate

Which will run your migrations and creates your tables.

  • Related