Laravel 12 Create, Skip, Run and Rollback Migrations

In this post ‘Laravel 12 Create, Skip, Run and Rollback Migrations’, I will show you how to create migrations, rollback a specific migration, and run a specific migration file.

In this laravel tutorial, I will show you step by step with examples how to create, skip, run, and rollback migrations.

What is a Migration in Laravel?

A migration like a version control system for your database. It allows the team to define and modify the structure of the database over time through code instead of making manual changes.

Migration files are created in the database/migrations folder in Laravel.

How to Create a Migration?

To create a new migration, use the following crtisan command:

 php artisan make:migration create_doctors_table

This command will create a file like: database/migrations/2025_07_14_134057_create_doctors_table.php , Inside the file, you’ll find two methods:

<?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('doctors', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }

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

  • The up() method is used to apply the changes.
  • The down() method is used to reverse the changes.

How to Run Migrations?

To run migration use the following command:

php artisan migrate
How to Run Migrations
Running Migrations

This command will execute all migrations that haven’t been run yet. It also tracks which migrations have already been executed by creating a migrations table in your database.

How to Run a Specific Migration?

The above command php artisan migrate will run all pending migration files that have not been executed yet. However, if you want to run only a specific migration file, you need to use the migration command with the file path option.

Execute the following command to run a specific migration:

php artisan migrate --path=database/migrations/0001_01_01_000000_create_users_table.php
How to Run a Specific Migration?
Run Specific Migration

This command only migrate users table and skip all other migration files.

How to Make Model With Migration?

To create a model along with its corresponding migration file, you can use the following command:

php artisan make:model ModelName -m

Here:

  • ModelName: The name of the model you want to create.
  • -m or –migration: This option tells Laravel to generate a migration file along with the model.

Example:

php artisan make:model Payment -m

After running this command, Laravel will generate two files:

   INFO  Model [project-path/app/Models/Payment.php] created successfully.

   INFO  Migration [project-path/database/migrations/2025_07_15_042957_create_payments_table.php] created successfully.  

Model File:

app/Models/Payment.php

Migration File:

database/migrations/2025_07_15_042957_create_payments_table.php

Skipping Migrations Conditionally in Laravel (Using shouldRun())

In this example, we’ll learn how to skip running migrations based on specific conditions in Laravel.

Sometimes you want to skip certain migration files from running due to specific reasons—like environment differences or temporary changes. When using feature flags , you may also want some database changes to apply only when a certain feature is active.

Before, this required manual steps or complicated logic to handle. But now, Laravel makes this easier with the new shouldRun() method. It lets each migration file decide for itself whether it should be executed, based on your own conditions.

Let’s take an example:

I want to skip the migration for the travel_plans table under certain conditions. To achieve this, I’ll use the shouldRun() method in the travel_plans migration file.

I’ll also add a variable in the .env file named SKIP_TABLE_TRAVEL. When this variable is set and its condition is met, the migration will be skipped automatically without showing any error.

In the code below, I used config(‘app.skip_table_travel’) to retrieve a boolean value (true or false) from the config file. This works correctly, but when trying to access the .env variable directly using env(‘SKIP_TABLE_TRAVEL’), it returns null.

<?php

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

return new class extends Migration
{

    public function shouldRun(): bool
    {
        return config('app.skip_table_travel')===true;
    }
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('travel_plans', function (Blueprint $table) {
            $table->id();
            $table->string('trip_name');
            $table->json('destination_countries'); // e.g., ["FR", "IT", "DE"]
            $table->timestamps();
        });
    }

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

.env file

SKIP_TABLE_TRAVEL=true

config/app.php

<?php

return [
...........
.......................................................
 'my_feature_enabled' => env('MY_FEATURE_ENABLED', false),
...........................................................
];

When you run the command: php artisan migrate the output look like this

Skip migration example
Skip travel plans table migration

The shouldRun() method works consistently with both the migrate and rollback commands. That means if a migration is skipped during php artisan migrate, it will also be skipped during php artisan migrate:rollback, helping maintain database integrity across all environments.

How to Rollback Migrations?

Laravel provides several Artisan commands to rollback database migrations, which is useful when you want to undo recently created table —whether for testing, development, or fixing mistakes.

Run the below command to rollback the last batch of migrations that were run.

 php artisan migrate:rollback
migration rollback batch example
migration rollback via batch example

This command will roll back or remove all the batch 3 tables.

Rollback Migrations Using Step

You can rollback limited number of migrations by using the step option with rollback command. For example the command given below will rollback the last 2 migrations.

 php artisan migrate:rollback --step=2
rollback the last 2 migrations
Rollback The Last 2 Migrations

Rollback Migrations Using Batch

You can roll back a specific batch of migrations using the –batch option with the migrate:rollback command. By default, php artisan migrate:rollback only rollback the latest batch of migrations. However, by specifying the –batch option, you can target and roll back a specific batch. For example ,the command given below will roll back all migrations in batch three:

php artisan migrate:rollback --batch=3
Rollback all migrations in Batch 3
Rollback all migrations in Batch 3

Rollback Migrations Using Reset

php artisan migrate:reset rollback all of your application’s migrations, reverting the database to its initial state. For Example, the command given below will rollback all the migrations.

 php artisan migrate:reset

Rollback And Migrate Using Refresh

When you run the php artisan migrate:refresh command, it rollback all migrations and then run the migrate command. This process re-creates the entire database structure.

 php artisan migrate:refresh

Rollback And Migrate Using Refresh With –Seed

The php artisan migrate:refresh –seed command is used to reset your database and re-run all of your migrations along with the database seeders.

This command performs three key actions in sequence:

  • Rollback all existing migrations
  • Re-runs all migrations to rebuild the database schema
  • Executes the database seeders defined in DatabaseSeeder.php
 php artisan migrate:refresh --seed

You can rollback and migrate limited number of migrations/tables using the –step option with migrate:refresh. For example, The command given below will rollback and re-migrate last 3 migrations.

php artisan migrate:refresh --step=3