Laravel 11 Create, Run and Rollback Migration

In this guide, we'll see how to create, run, and roll back migration in laravel 11. Migrations are like version control for your database. Also, see run specific migration in laravel 11 and roll back specific migration in laravel 11.

A migration class contains two methods: up and down. The up method is used to add new tables, columns, or indexes to your database, while the down method should reverse the operations performed by the up method.

Create Migration

First, we'll create a migration using the following command.

php artisan make:migration create_blogs_table

After running the above command, you will see a new file in the database/migration folder.

database/migrations/2024_04_06_144031_create_blogs_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.
     */
    public function up(): void
    {
        Schema::create('blogs', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->text('body');
            $table->boolean('is_published')->default(0);
            $table->timestamps();
        });
    }
    
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('blogs');
    }
};

 

Run Migration

Then, we'll migrate the migration into the database using the following command.

php artisan migrate

 

 

Run Specific Migration

Run a specific migration using the following command.

php artisan migrate --path=/database/migrations/2024_04_06_144031_create_blogs_table.php

 

Migration Rollback

Rollback a migration using the following command:

php artisan migrate:rollback

 

Rollback Specific Migration

Roll back a specific migration using the following command.

php artisan migrate:rollback --path=database/migrations/2024_04_06_144031_create_blogs_table.php

 

Rest Migration

Reset migration will roll back all the application's migrations.

php artisan migrate:reset

 

Column-modifiers

In addition to the column types listed above, there are several column "modifiers" you may use when adding a column to a database table. 

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
Schema::table('users', function (Blueprint $table) {
    $table->string('email')->nullable();
});

 

Dropping Columns

To drop a column, you may use the dropColumn method on the schema builder:

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn('votes');
});

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn(['votes', 'avatar', 'location']);
});

 


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS