A Close Look at Laravel 7 Migration Types | TheDevNerd

How to use Different Types of Migration in Laravel 7 and How to run a Specific Migration

Types of Migration in Laravel 7 [ Explained ]  - StudyWithKishan


             In this article we will cover different types of migration in Laravel 7 with Example Like migrate:rollback , migrate:fresh , migrate:refresh , migrate:status etc. Laravel Eloquent provides very simple migration commands to work with your database tables .

             Here we will learn migration commands with examples and we will be using Laravel 7 . So let's dive in and see different types of Migrations in Laravel with php artisan command.


Table of Commands : -

  • Making model with migration table
  • Configure at migration file
  • Migrating table
  • Migration status
  • Migration Rollback
  • Migration Fresh and Refresh
  • Laravel Specific table migration

Making Model with Migration table :

       In Laravel 7 , to create a migration table we need to create a model first . You first need to create a model with the migration file . You can use the following command to create a model with migration file .


php artisan make:model model_name -m

Types of Migration in Laravel 7 [ Explained ]  - StudyWithKishan


This command will create your model at ( app\model_name.php ) as well as a migration file at ( app\database\migrations\2020_07_28_105702_create_model_names_table.php ) .


Configuring Migration file  :

      The next step is just open your migration file and you will see there are two function up() and down() , inside the up() function add fields that you want in your table as shown in the following .


<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDemosTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('demos', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('address');
            $table->integer('phone');
            $table->string('about');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('demos');
    }
}

Migrating Table :

Now your table structure is ready just use the following command to create the table in your database .


php artisan migrate

Types of Migration in Laravel 7 [ Explained ]  - StudyWithKishan

Migration Status :

 You can use the following commands to see total number of migration tables or files available in your project and their details .


php artisan migrate:status

Types of Migration in Laravel 7 [ Explained ]  - StudyWithKishan


Migration Rollback :

There are basically two types of common Rollback methods provided by Laravel which are as follows

Single Rollback :

 Suppose you make a migration table and then you realize that you make some mistake during making the table fields ,  in that case you can simple use this command to rollback and go back to your previous table structure .


php artisan migrate:rollback

Stepped Rollback :

In this rollback you can travel to the number of previeous  migration you had made .


php artisan migrate:rollback --step=4

Reset Rollback :

 This command will simply rollback all the migration that you have made in your application .


php artisan migrate:reset

Types of Migration in Laravel 7 [ Explained ]  - StudyWithKishan

Migration Fresh and Refresh :

Migration Fresh :

This command will drop / delete all your tables and again migrate all the tables means it will delete all your database table and re-create it .


php artisan migrate:fresh


Migration Refresh :

This command will rollback your migration to the previous one and then again migrate those tables .


php artisan migrate:refresh

Specific Table Migration :

This command is very much helpful when you want to migrate a specific table . Suppose you made some changes on a perticular migration file and you just want to update the table structure of that perticular file , then in that case you can use this command for Laravel run specific migration.


php artisan migrate:refresh --path=/database/migrations/fileName.php

Types of Migration in Laravel 7 [ Explained ]  - StudyWithKishan


          These are the most used migration commands during developed and i hope this article helped you.

Thank you for reading this article 😊

For any query do not hesitate to comment 💬

Also Read :



Get average in Laravel Eloquent

Search functionality in Laravel

Laravel 7/6 Email Verification

Why Laravel is so Popular PHP Framework



Previous Post Next Post

Contact Form