Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

You can also use a wildcard, like so:

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

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

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

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.

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'.

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'
]);

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

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

This works at laravel 8

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

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

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.

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/**/*');
    }


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

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/');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top