Laravel Tutorials

Implement Conditional Migrations in Laravel with the New shouldRun() Method

Published
Implement Conditional Migrations in Laravel with the New shouldRun() Method image

Laravel solves the challenge of managing migrations with feature flags through the new shouldRun() method. This elegant addition allows migrations to execute conditionally based on application state, simplifying phased rollouts and feature management.

When implementing feature flags or staged deployments, certain database changes should only apply when specific features are active. Previously, developers relied on complex workarounds or manual migration tracking. The shouldRun() method provides a clean solution by allowing migrations to determine their own execution criteria:

return new class extends Migration
{
    public function shouldRun()
    {
        return Feature::active(Bookings::class);
    }
    
    public function up()
    {
        Schema::create('bookings', function (Blueprint $table) {
            $table->id();
            $table->string('location');
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::dropIfExists('bookings');
    }
}

This migration executes only when the 'Bookings' feature is active. When inactive, the migration gets skipped without errors, and no entry appears in the migrations table.

The method proves particularly valuable for multi-tenant applications or gradual feature rollouts:

// 2023_06_22_create_notification_tables.php
return new class extends Migration
{
    public function shouldRun()
    {
        // Only run this migration for environments or tenants
        // with the notification system enabled
        return config('features.advanced_notifications') === true;
    }
    
    public function up()
    {
        Schema::create('notification_templates', function (Blueprint $table) {
            $table->id();
            $table->string('channel');
            $table->json('content');
            $table->timestamps();
        });
        
        Schema::create('notification_delivery', function (Blueprint $table) {
            $table->id();
            $table->string('recipient');
            $table->json('metadata');
            $table->timestamps();
        });
    }
    
    public function down()
    {
        Schema::dropIfExists('notification_delivery');
        Schema::dropIfExists('notification_templates');
    }
};

The shouldRun() method works consistently with both migrate and rollback commands. Migrations skipped during migration will also be skipped during rollback operations, maintaining database integrity.

By implementing the shouldRun() method, developers create more adaptable, context-aware migrations that respond to application requirements, making feature management and progressive deployment strategies more effective and manageable.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
UUID Request Input and Migration Failure Events in Laravel 13.42 image

UUID Request Input and Migration Failure Events in Laravel 13.42

Read article
Replay Missed Laravel Scheduled Tasks With Schedule Catchup image

Replay Missed Laravel Scheduled Tasks With Schedule Catchup

Read article
Search Polymorphic Relations and Filament Tables With Laravel Searchable image

Search Polymorphic Relations and Filament Tables With Laravel Searchable

Read article
Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs image

Laravel Rest Api 3.0: Typed Fields, Metrics, and OpenAPI Docs

Read article
Real-Time Inertia Props With Laravel Reverb image

Real-Time Inertia Props With Laravel Reverb

Read article
PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support image

PhpStorm 2026.3 Adds an Agent Workspace and PHP 8.6 Support

Read article