In Laravel, Migration provides a way for easily sharing the schema of the database. It also makes the modification of the schema much easier. It is like creating a schema once and then sharing it many times. It gets very useful when you have multiple tables and columns as it would reduce the work over creating the tables manually.
To Create Migration: It can be created by using the artisan command as shown below:
php artisan make:migration create_articles_table
Here, articles are going to be the table and in place of that you can write any other table name which you want to create and is appropriate for the application but the table name as you saw, is to be in plural form. So, it is written as articles and not article. This is the naming scheme used by Laravel and it is important to specify create at the start and table at the end according to the naming scheme of a Migration file in Laravel.
All the migration file that we create using the artisan command are located at database/migrations directory. So, after we run the above command, it will generate a PHP file with the name we specified with the current date and time.
And the file will be created with some predefined class and functions as shown in the code below:
If you want to specify the name of the table different than the name that you specified as the file name then you can use an option as –create with the command as follows:
php artisan make:migration create_articles_table --create=gfg
With this command, the table name contained in the create() method will be gfg and not articles, which is specified in the name of the file.
Basic Structure of a Migration: A migration file contains a class with the name of the file specified while creating the migration and it extends Migration. In that we have two functions, the first one is up() function and the second one is down() function. The up() is called when we run the migration to create a table and columns specified and the ‘down()’ function is called when we want to undo the creation of ‘up()’ function.
In the up() function, we have create method of the Schema facade (schema builder) and as we say before, the first argument in this function is the name of the table to be created. The second argument is a function with a Blueprint object as a parameter and for defining the table.
In the down() function, we have a dropIfExists method of the schema builder which when called will drop the table.
To Run Migration: Before running a migration, we first have to create a MySQL Database and Connect to it. After that is done, to Run a Migration, we can use an Artisan command as follows:
php artisan migrate
This command will run the up() function in the migration class file and will create the table with the all columns specified.
Note: This command will run the up() function and create all the tables in the database for all the migration file in the database/migrations directory.
To rollback any last migration which is done, we can use the following Artisan command:
php artisan migrate:rollback
This command will run the down() function in the migration class file.
- Laravel | Routing Basics
- Laravel | View Basics
- Laravel | Controller Basics
- Laravel | Eloquent Model Basics
- Laravel | Artisan Commands to know in Laravel
- Laravel | Front-end Scaffolding
- Laravel | Delete Records
- Differences Between Django vs Laravel
- Laravel | CSRF Protection
- Laravel | Directory Structure
- Laravel | Installation and Configuration
- Laravel | Validation Rules
- Laravel | Artisan Console Introduction
- Difference Between Laravel and Ruby on Rails
- What are the differences and Similarities Between Lumen and Laravel?
- Difference Between Laravel and CodeIgniter Framework in PHP
- Laravel | Blade Templates Inheritance
- Laravel | MySQL Database Connection
- Different ways for passing data to view in Laravel
- How to upload Laravel App to Heroku Cloud Application Platform