4

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?

Adam
  • 25,960
  • 22
  • 158
  • 247
  • if you need to migrate all folders in migrations folder use `php artisan migrate --path=/database/migrations/*` – STA Jul 24 '20 at 09:35
  • 1
    @STA `--path=/database/migrations/*` will only trigger migrations in subfolders. In other Laravelapplication that I have, migrations from subfolders are always fetched and executed in correct order (by filename). I thought its maybe a setting issue. – Adam Jul 24 '20 at 09:46
  • 1
    Maybe create a custom command that will run migrations on all the folders/sub folders? also https://stackoverflow.com/a/21643259/11801683 – jewishmoses Jul 24 '20 at 09:53
  • Never done this, but make sure you don't accidentally end up with two migrations with the same name, i.e. `2014_10_12_000000_create_users_table`, not sure how this will be handled when inserting migration names to the migrations table. – brombeer Jul 24 '20 at 10:26
  • @jewishmoses your right. https://stackoverflow.com/a/43791121/2311074 is what I had in my other Laravel installation, that was why it worked there without problems :) – Adam Jul 24 '20 at 11:48

2 Answers2

2

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.

Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
0

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.

Fahd Yousri
  • 401
  • 3
  • 12