How to intervene Laravel's default migration run order?

2 days ago 1
ARTICLE AD BOX

I am currently trying to develop a addon-oriented CMS with Laravel, but I hit a wall when I tried to intervene with Laravel's default migration order. Laravel, by default runs your migration orders by their filename timestamp prefix, but in my case CMS migration order is always core -> addons -> app, but I couldn't find any solution to this problem just yet.

I know I can just publish and barf all files to app side while changing their filename timestamps based on their group, but I would really prefer to not to show any core logic to the app side.

What I want to do:

Intervene any artisan call that depends on migration order (e.g. migration.run, migration.down etc.) and change the default file sorting logic.

What I tried so far:

I created a Migrator file that extends the Laravel's default migrator

<?php namespace CMS\Database\Migrators; use Illuminate\Database\Migrations\Migrator as LaravelMigrator; class Migrator extends LaravelMigrator { public function run($paths = [], array $options = []): array { dd("debug"); // never fires on any db related artisan call $this->runCore(); $this->runAddons(); return []; } }

then I singleton it inside a service provider's register method (that comes before any db related service provider, which shouldn't matter)

<?php namespace CMS\Providers; use Illuminate\Database\Migrations\MigrationRepositoryInterface; use Illuminate\Support\ServiceProvider; use Illuminate\Database\Migrations\Migrator as LaravelMigrator; use CMS\Database\Migrators\Migrator as CMSMigrator; class DatabaseServiceProvider extends ServiceProvider { public function register(): void { $this->app->singleton(LaravelMigrator::class, function ($app) { return new CMSMigrator( $app['files'], $app['db'], $app['events'], $app->make(MigrationRepositoryInterface::class) ); }); } }

Software Information

{ "laravel/framework": "^12.0", "laravel/tinker": "^2.10.1" }

Thank you.

Read Entire Article