DEV Community

Cover image for Laravel 12 Create, Skip, Run and Rollback Migrations
itstuffsolutions
itstuffsolutions

Posted on

Laravel 12 Create, Skip, Run and Rollback Migrations

In this guide on "Laravel 12: How to Create, Skip, Run, and Rollback Migrations", you'll learn how to work with Laravel migrations effectively. We’ll walk through the process of generating migration files, executing only specific ones, skipping certain migrations, and rolling back individual migration files when needed.

This Laravel tutorial provides clear, step-by-step examples to help you understand each command in depth. For additional guidance and advanced usage, don’t forget to visit the official Laravel migration documentation.
What You'll Learn in This Guide

In this post, you'll explore the following topics related to Laravel migrations:

1. How to Run a Specific Migration
2. How to Create a Model Along with a Migration
3. How to Conditionally Skip Migrations in Laravel using shouldRun()
4. How to Rollback Migrations using different methods:

  • Rollback using --step
  • Rollback using migrate:reset
  • Rollback using --batch
  • Rollback and re-run using migrate:refresh
  • Rollback and seed using migrate:refresh --seed
  • Rollback a specific migration file
  • Rollback a specific migration and re-migrate it

Each method is explained with examples to help you manage your database schema more efficiently in Laravel 12.

What is a Migration in Laravel?

In Laravel, a migration acts like a version control system for your database. It enables developers to define, track, and modify the structure of the database using code — rather than editing it manually. This makes collaboration easier and ensures consistency across different environments.

Migration files are stored in the database/migrations directory, and each file represents a specific change or update to the database schema.

php artisan make:migration create_doctors_table
Enter fullscreen mode Exit fullscreen mode

This command will generate a new migration file in the database/migrations directory, with a timestamp in the filename. For example:** database/migrations/2025_07_14_134057_create_doctors_table.php**
Inside the generated file, you’ll find two predefined 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');
    }
};

Enter fullscreen mode Exit fullscreen mode

*up() *– defines the changes to apply to the database (e.g., creating a table or adding a column).

down() – defines how to reverse those changes (e.g., dropping the table or removing the column).
How to Run Migrations?
To run all pending migrations, use the following Artisan command:

php artisan migrate
Enter fullscreen mode Exit fullscreen mode

This command will execute all migration files that haven’t been run yet. Laravel keeps track of which **migrations **have already been executed by creating a special migrations table in your database. This ensures that each migration runs only once, avoiding duplicate changes.

How to Run a Specific Migration?
The command php artisan migrate runs all pending migration **files that haven’t been executed yet. However, if you want to **run only a specific migration, you can do so by specifying the file path using the --path option.
Use the following command:


php artisan migrate --path=database/migrations/0001_01_01_000000_create_users_table.php

Enter fullscreen mode Exit fullscreen mode

Make sure the path is relative to the base directory of your Laravel project. This command will run only the specified migration file, without affecting others.

How to Make Model With Migration?

To generate a model along with its corresponding migration file, use the following Artisan command:

php artisan make:model ModelName -m
Enter fullscreen mode Exit fullscreen mode

here:
ModelName: The name of the model you want to create (e.g., Payment).

-m or --migration: This flag tells Laravel to automatically create a migration file alongside the model.

Example:

php artisan make:model Payment -m
Enter fullscreen mode Exit fullscreen mode

After running the command above, Laravel will generate:

  1. A model file:
    app/Models/Payment.php

  2. A migration file:
    database/migrations/2025_08_08_XXXXXX_create_payments_table.php
    (The timestamp will vary depending on when you run the command.)

📖 Read the full blog post here:
👉 Laravel 12 Create, Skip, Run and Rollback Migrations

You might also like:

Top comments (0)