Migrate Specific Migration File in Laravel 9 Tutorial

Reading Time: 3 minutes
4,350 Views

When you create migrations in laravel, you also have command to migrate it and create tables schemas in database. Migration files are the class files 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 9. This command is not laravel 9 specific, you can use it in with any version.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your 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, you should see into /database/migrations folder, 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 2022_04_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;

return new class 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

Using Migrate, we create table structures into database. 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

To Migrate a specific migration file,

Syntax:

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

Example:

$ php artisan migrate:refresh --path=database/migrations/2022_04_01_092040_create_products_table.php

Inside this case, migration file path is /database/migrations/2022_04_01_092040_create_products_table.php

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

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.