Migrate Specific Migration File in Laravel 8 Tutorial

Share this Article
Reading Time: 3 minutes
3,661 Views

When you create migrations in laravel, you also have command to migrate it and create tables schemas in database. Migration file is the file which creates the table structure.

When we use migrate command from artisan command list, it will run all migrations and create table. But in some cases we want to run only a specific migration instead to run all.

Inside this article we will see the concept of migrate specific migration file in laravel 8. This command is not laravel 8 specific, you can use it in with any version.

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project.

If your system doesn’t has composer Installed, Learn Composer Installation Steps.

Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Create Migration

By default after fresh installation of laravel, when you will see into /database/migrations folder you should see 3 default migration files.

To create new migration file, here is the artisan command.

Open project into terminal and run this command.

$ php artisan make:migration create_products_table

It will create a migration file with name like 2021_05_01_092040_create_products_table.php inside /database/migrations folder.

Open migration file and write this complete code.

<?php

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

class CreateProductsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string("title", 120);
            $table->integer("amount");
            $table->string("prouduct_thumbnail", 170)->nullable();
            $table->text("description");
            $table->timestamps();
        });
    }

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

Migrate Migrations in Laravel

We have artisan command to migrate laravel migration files.

Migrate All Migrations

$ php artisan migrate

It will migrate all migrations from /database/migrations folder create their respective tables.

Migrate a Specific Migration File

$ php artisan migrate:refresh --path=database/migrations/2021_05_01_092040_create_products_table.php

Syntax – $ php artisan migrate:refresh –path=<MIGRATION FILE PATH>

Inside this case, migration file path is database/migrations/2021_05_01_092040_create_products_table.php

We hope this article helped you to learn about Migrate Specific Migration File in Laravel 8 Tutorial Example in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here