Polyscope - The agent-first dev environment for Laravel

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

Last updated on by

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

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.

image
Laravel Code Review

Get expert guidance in a few days with a Laravel code review

Visit Laravel Code Review
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
PhpStorm logo

PhpStorm

The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

PhpStorm
Laravel Cloud logo

Laravel Cloud

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

Laravel Cloud
Acquaint Softtech logo

Acquaint Softtech

Acquaint Softtech offers AI-ready Laravel developers who onboard in 48 hours at $3000/Month with no lengthy sales process and a 100 percent money-back guarantee.

Acquaint Softtech
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
Harpoon: Next generation time tracking and invoicing logo

Harpoon: Next generation time tracking and invoicing

The next generation time-tracking and billing software that helps your agency plan and forecast a profitable future.

Harpoon: Next generation time tracking and invoicing
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
MongoDB logo

MongoDB

Enhance your PHP applications with the powerful integration of MongoDB and Laravel, empowering developers to build applications with ease and efficiency. Support transactional, search, analytics and mobile use cases while using the familiar Eloquent APIs. Discover how MongoDB's flexible, modern database can transform your Laravel applications.

MongoDB

The latest

View all →
Ship AI with Laravel: Stop Your AI Agent from Guessing image

Ship AI with Laravel: Stop Your AI Agent from Guessing

Read article
Laravel Cloud Adds Path Blocking to Prevent Bots From Waking Hibernated Apps image

Laravel Cloud Adds Path Blocking to Prevent Bots From Waking Hibernated Apps

Read article
Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions image

Making Laravel MongoDB Operations Idempotent: Safe Retries for Financial Transactions

Read article
FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0 image

FormRequest Strict Mode and Queue Job Inspection in Laravel 13.4.0

Read article
Pretty PHP Info: A Modern Replacement for `phpinfo()` image

Pretty PHP Info: A Modern Replacement for `phpinfo()`

Read article
Laracon US 2026 Announced image

Laracon US 2026 Announced

Read article