Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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 :
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.