45

So my migrations folder looks like this since I have dozens of tables it keeps things organized and clean:

migrations/
  create_user_table.php
  relations/
  translations/

I'm trying to do a refresh all migrations and seed but it seems like I've run into a slight hiccup where I don't know the artisan command to run migrations recursively (i.e. run migrations in the relations and translations folders as well).

I've tried adding --path="app/database/migrations/*" however it spat out an error. Does anyone know the solution to this?

3
  • After digging around the web, looks like it's impossible to do: github.com/laravel/framework/issues/2561 Feb 8, 2014 at 3:58
  • Is there a reason why not sorting/organizing your migrations is a good thing? Feb 8, 2014 at 3:59
  • function rei($folder) { $iterator = new DirectoryIterator($folder); system("php artisan migrate --path=" . $folder); foreach ($iterator as $fileinfo) { if ($fileinfo->isDir() && !$fileinfo->isDot()) { echo $fileinfo->getFilename() . "\n"; rei($folder . $fileinfo->getFilename() . '/'); } } } rei('./database/'); Feb 26, 2016 at 4:06

15 Answers 15

64

The only way to do it right now is to manually go through all the migrations. That is, you have to run the migration command on each of your subfolders:

php artisan migrate --path=/app/database/migrations/relations  
php artisan migrate --path=/app/database/migrations/translations

However, what you can do is easily extend the artisan system to write your own migrate command that will iterate through all folders under the migrations folder, create these commands for you and run them.

You can also simply write a shell script if you don't want to get into doing this via artisan

Edit: for Laravel >= 5.0, the correct commands to migrate migration files in sub directories would be:

php artisan migrate --path=/database/migrations/relations
php artisan migrate --path=/database/migrations/translations

4
  • Thanks, I ended up going the script route. Feb 8, 2014 at 11:12
  • This helped me. Although I'm confused how would you go about rollback with this way? Jun 21, 2016 at 5:42
  • 1
    Same with php artisan migrate:rollback --path=migration/path Aug 15, 2018 at 7:46
  • I had to add .php at the end to make it work.
    – Black
    May 3, 2022 at 9:19
53

This add to boot method in AppServiceProvider

$mainPath = database_path('migrations');
$directories = glob($mainPath . '/*' , GLOB_ONLYDIR);
$paths = array_merge([$mainPath], $directories);

$this->loadMigrationsFrom($paths);

Now you use can php artisan migrate and also php artisan migrate:back

6
  • 5
    This is a really quick and easy solution to the problem. Don't have to think about it again, nor have a different bash script... just put your migrations in sub folders and run. Jun 18, 2018 at 16:55
  • 2
    $this should be the approved answer the path param is just a workaround :)
    – dev
    Sep 15, 2018 at 7:07
  • 2
    Could it happen that the migration files are not called in successive order? Image you have subfolder1/2018-10.php , subfolder1/2018-12.php and ``subfolder2/2018-11.php. WIll the migration files stil be called as 2018-10, 2018-11` and 2018-12?
    – Adam
    Oct 31, 2018 at 12:24
  • 2
    @Adam - I just tried it on Laravel 5.4 and it put the files in order based on the file names. So it ran them based on the file names (i.e. 2020_07_22_create....) regardless of which directory they came from. Jul 22, 2020 at 18:56
  • @turtlechief yes I made the same experience
    – Adam
    Jul 22, 2020 at 19:35
17

You can also use a wildcard, like so:

php artisan migrate --path=/database/migrations/*
0
5

You can use the following command to do this recursively:

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

**/* is also known as the globstar

Before this works you must check if your bash supports the globstar. You can do this by executing shopt and checking for globstar.

Globstar is supported by default by most server distributions but might not work on MAC.

For more on globstar see: https://www.linuxjournal.com/content/globstar-new-bash-globbing-option

5
  • Tested and it doesn't work. Just gives an error that it can't find the path. Dec 23, 2018 at 19:46
  • @MichaelRyanSoileau We have been running this in production for 2 years, what exactly is not working for you? What system are you using?
    – Mazzy
    Jan 3, 2019 at 15:02
  • PHP 7.3 running on mac, Laravel version 5.5. Every time I try that, it's a no-op. :( Jan 3, 2019 at 23:44
  • Ah okay, I have done some research and it shows that Mac's bash does not support globstar out of the box (apple.stackexchange.com/questions/291287/…). I myself am running homestead/vagrant and using the terminal in virtual box. But if you're using your mac's terminal globstar is not natively supported. I will edit my answer accordinly
    – Mazzy
    Jan 4, 2019 at 13:43
  • Ah, all is explained. Thanks for the due diligence. Jan 4, 2019 at 20:53
3

In Laravel 5 the database folder sits alongside the app folder by default. So you could run this to migrate a single folders migrations:

php artisan migrate --path=/database/migrations/users
3

You can try this package nscreed/laravel-migration-paths. By default all sub directories inside the migrations folder will be autoloaded. Even you can add any directories very easily.

'paths' => [
    database_path('migrations'),
    'path/to/custom_migrations', // Your Custom Migration Directory
],

Don't need any special command just the generic: php artisan migrate will do your tasks.

2
  • 2
    This is a nice package and I'm using it in my current project. It would be nicer if you added a blacklist feature. I don't really need it in my current project. However researching this functionality I came across a rejected feature request to Laravel and one of the stated reasons is that people often use a sub folder called "archive" to store older migration files. You might also want to blacklist that by default. Otherwise, thank you for your excellent work
    – kaan_a
    Oct 2, 2019 at 9:10
  • Thank you for your suggestion. :-) Oct 2, 2019 at 15:44
1

I rewrote the MigrationServiceProvider:

- registerResetCommand()
- registerStatusCommand()
- registerMigrateCommand()

There you can register your own commands:

class MigrateCommand extends Illuminate\Database\Console\Migrations\MigrateCommand

After that you just need to extend youd directories:

protected function getMigrationPaths()

Or you just register the paths on application boot. I've already done my solution before I knwewd about '$this->loadMigrationsFrom'.

1

A Simple solution is to create an Artisan Command for example (migrate:all),

then inside handle function define migrate command for each sub directories as mentioned bellow.

Artisan::call('migrate', [
   '--path' => '/database/migrations/employee'
]);
1

Only relative path works for me (in Laravel 5.7):

php artisan migrate --path=database/migrations/your-folder-with-migrations
1

This works at laravel 8

php artisan migrate --path=database/migrations/tenant

0

It is a not a "direct" solution but i suggest you to look at Modularity into your laravel project.

Modules can segment your application in several smaller "folders" and bring migration, seeds, classes, routes, controllers, commands together in easily maintainable folders.

This package is a good start : https://github.com/pingpong-labs/modules

0

No changes are necessary, whenever you need it, use this command:

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

If you want a shortcut, you can include that in your composer.json:

"scripts": {

    "migrate": [
        "@php artisan migrate --force --path=database/migrations/*"
    ]
}

Then just use composer migrate in the terminal.

0

add in app/Providers/AppServiceProvider.php the path of your migration folder, you can add a string or an array

    public function register()
    {
        $this->registerMigrations();
    }

    protected function registerMigrations()
    {
        return $this->loadMigrationsFrom(__DIR__ . '/../../database/migrations/**/*');
    }


-1

A Simple Laravel solution is to create a gulp task (say migrate-others).

var elixir = require('laravel-elixir');
var gulp = require('gulp');
var shell = require('gulp-shell')

/*
 |--------------------------------------------------------------------------
 | Elixir Asset Management
 |--------------------------------------------------------------------------
 |
 | Elixir provides a clean, fluent API for defining some basic Gulp tasks
 | for your Laravel application. By default, we are compiling the Sass
 | file for our application, as well as publishing vendor resources.
 |
 */

elixir(function(mix) {
    mix.sass('app.scss');
});

// Our Task
gulp.task('migrate-others', shell.task([
    'php artisan migrate --path=/app/database/migrations/relations',
    'php artisan migrate --path=/app/database/migrations/translations',
]));

Now you can simply call

gulp migrate-others
3
-1

Here you go!

 function rei($folder)
    {
        $iterator = new DirectoryIterator($folder);
        system("php artisan migrate --path=" . $folder);
        foreach ($iterator as $fileinfo) {
            if ($fileinfo->isDir() && !$fileinfo->isDot()) {
                echo $fileinfo->getFilename() . "\n";
                rei($folder . $fileinfo->getFilename() . '/');
            }
        }
    }

 rei('./database/');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.