For everyone who is still interested in this, the Laravel 5: Laravel update has implemented the ability to run one migration file at a time (in version 5.7).
Now you can run this: php artisan migrate --path=/database/migrations/my_migration.php (as answered here )
Since Illuminate\Database\Migrations\Migrator::getMigrationFiles() now contains this code: return Str::endsWith($path, '.php')? [$path]: $this->files->glob($path.'/*_*.php'); return Str::endsWith($path, '.php')? [$path]: $this->files->glob($path.'/*_*.php'); return Str::endsWith($path, '.php')? [$path]: $this->files->glob($path.'/*_*.php'); (see source code .)
But in my case, I really wanted to run a set of migrations at the same time, and not just one or all .
So I followed the Laravel path and registered another Migrator implementation that decides which files to use:
/** * A migrator that can run multiple specifically chosen migrations. */ class MigrationsSetEnabledMigrator extends Migrator { /** * @param Migrator $migrator */ public function __construct(Migrator $migrator) { parent::__construct($migrator->repository, $migrator->resolver, $migrator->files); // Compatibility with versions >= 5.8 if (isset($migrator->events)) { $this->events = $migrator->events; } } /** * Get all of the migration files in a given path. * * @param string|array $paths * @return array */ public function getMigrationFiles($paths) { return Collection::make($paths)->flatMap(function ($path) { return Str::endsWith($path, ']') ? $this->parseArrayOfPaths($path) : (Str::endsWith($path, '.php') ? [$path] : $this->files->glob($path . '/*_*.php')); })->filter()->sortBy(function ($file) { return $this->getMigrationName($file); })->values()->keyBy(function ($file) { return $this->getMigrationName($file); })->all(); } public function parseArrayOfPaths($path) { $prefix = explode('[', $path)[0]; $filePaths = explode('[', $path)[1]; $filePaths = rtrim($filePaths, ']'); return Collection::make(explode(',', $filePaths))->map(function ($filePath) use ($prefix) { return $prefix . $filePath; })->all(); } }
We need to register it in the container as 'migrator' (so that it is available as $app['migrator'] ), because that's how the Migrate team accesses it when it registers itself in IoC. To do this, we put this code in the service provider (in my case, it is DatabaseServiceProvider ):
public function register() { $this->app->extend('migrator', function ($migrator, $app) { return new MultipleSpecificMigrationsEnabledMigrator($migrator); });
Then you can run this:
php artisan migrate --path=[database/migrations/my_migration.php,database/migrations/another_migration.php]
Pay attention to several comma-separated migration files.
It is tested and works in Laravel 5.4 and should be compatible with Laravel 5.8.
What for?
For all who are interested: the use case updates the version of the database along with its data.
Imagine, for example, that you want to combine the street and house number of all users into a new column, let's call it street_and_house . And imagine that you want to do this on several installations in a safe and proven way - you will probably create a script for this (in my case, I create data version control commands - artisan teams).
To perform this operation, you must first load users into memory; then start the migration to remove the old columns and add new ones; then assign street_and_house=$street. " ". $house_no to each user street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no street_and_house=$street. " ". $house_no and save users. (I am simplifying here, but you can certainly imagine other scenarios)
And I do not want to rely on the fact that I can start all migrations at any given time. Imagine that you want to update it, say, from 1.0.0 to 1.2.0, and there were several packages of such updates - performing any other migrations can damage your data, because these migrations must be processed by their own dedicated update command. Therefore, I want to run only selected known migrations with which this update knows how to work, then perform data operations, and then, possibly, execute the following data update command. (I want to be as protective as possible).
To achieve this, I need the aforementioned mechanism and define a fixed set of migrations that must be performed for such a team to work.
Note: I would prefer to use a simple decorator using the __call magic method and avoid inheritance (a similar mechanism that Laravel uses in \Illuminate\Database\Eloquent\Builder to migrate \Illuminate\Database\Query\Builder ), but MigrateCommand unfortunately, MigrateCommand requires the presence of a Migrator instance in its constructor.
One final note: I wanted to post this answer to the question " How to start a specific migration in laravel , since it is specific to Laravel 5?". But I canβt - since this question is marked as a duplicate of this (although this question is marked as Laravel 4).