Open In App

Laravel | Migration Basics

Improve
Improve
Like Article
Like
Save
Share
Report

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:




<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreateArticlesTable extends Migration
{
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
        });
    }
  
    public function down()
    {
        Schema::dropIfExists('articles');
    }
}


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.




<?php
  
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
  
class CreateArticlesTable extends Migration
{
    public function up()
    {
        Schema::create('gfg', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
        });
    }
    public function down()
    {
        Schema::dropIfExists('gfg');
    }
}


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.

Reference: https://laravel.com/docs/6.x/migrations



Last Updated : 30 Dec, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads