How to migrate a specific table in Laravel?

In this tutorial, I will give you an example of How to migrate a specific table in Laravel, So you can easily apply it with your Laravel 5, Laravel 6, Laravel 7, Laravel 8, and Laravel 9 application.

First, what we’re doing here, This is the example :


Run specific migration Laravel

We will discuss how to migrate a specific table in laravel, for example, we will create a products table and add the name and products fields to the table

If in case we want to change the name field to product_name in the products table, for this action laravel provides a refresh method.

Generating Migration:-

php artisan make:migration create_products_table

Migration Structures :

<?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('name');
            $table->longText('description');
            $table->timestamps();
        });
    }

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

Run Migration :

php artisan migrate

Now update the name column to product_name in the products table.

database\migrations\2022_09_16_111754_create_products_table.php

<?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('product_name');
            $table->longText('description');
            $table->timestamps();
        });
    }

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

Refresh Migration:-

php artisan migrate:refresh --path=database/migrations\2022_09_16_111754_create_products_table.php

Output:-

In this article, we learned “How to Run specific migration laravel example”, I hope this article will help you with your Laravel application Project.

Read also:- How to add foreign key in Laravel 9 migration.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top