artisan make:migration ignores subfolders?

Viewed 205

I am using Laravel 5.6 and and migration files inside database/migrations/ work when I call artisan migrate but migration files in subfolders like database/migrations/example are skipped.

Even artisan migrate:status does not show the subfolders.

How can I tell Laravel to also include migration files from subfolders?

2 Answers

vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php line 465

same line number since Laravel 5.7 I guess, there's this line:

return Str::endsWith($path, '.php') ? [$path] : $this->files->glob($path.'/*_*.php');

This looks for the root migrations path matching the *_*.php pattern. If it was **/*_*.php you would've accomplished what you wanted.


But, good news! It can be hacked! The method responsible for getting the paths:

vendor\laravel\framework\src\Illuminate\Database\Console\Migrations\BaseCommand.php line 19

says that the path argument you gave to the command, will not be sanitized, or modified,

    if ($this->input->hasOption('path') && $this->option('path')) {
        return collect($this->option('path'))->map(function ($path) {
            return ! $this->usingRealPath()
                            ? $this->laravel->basePath().'/'.$path
                            : $path;
        })->all();
    }

So, setting the path argument as database/migrations/** should enable you to recurse into subfolders.

php artisan migrate --path="database/migrations/**"

Note: Then, for every migrate[:*] command to work, you need to specify the --path attribute. I migrated and rolled back with the --path attribute, and it worked.

There's a method in ServiceProvider that can do that for you.

loadMigrationsFrom($paths)

You will need to call it in your AppServiceProvider in the boot method like so:

public function boot()
{
    $paths = [your, custom, migration, directories];
    $this->loadMigrationsFrom($paths);
}

After doing this, running php artisan migtate will take subdirectories in consideration.

Related