14

I am trying to put laravel database migrations in sub folders, but not sure how to do this.

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
Veljko Sirovljević
  • 199
  • 1
  • 1
  • 13

4 Answers4

30

When creating new migrations and executing your migrations, you can pass in a path parameter through the command line interface to specify the directory it will use to create and run the migrations respectively.

php artisan make:migration create_users_table --path=/path/to/your/migration/directory

php artisan migrate --path=/path/to/your/migration/directory
Liam Hammett
  • 1,631
  • 14
  • 18
14

In AppServiceProvider, find a boot method and add there following

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

Now you can use php artisan migrate and also php artisan migrate:back when having migrations in sub folders

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
4

If you eager to do this, here is a quick solution at nscreed/laravel-migration-paths pakcage. Very simple and easy to use.

During the periodical development phase the migrations folder may become very large. It is very helpful if we can organize the content of the migration folders. This library helps to organize migration files in different folders. Even, if your organize your existing files it will works as well.

Hope, it will help you.

sybozz
  • 867
  • 8
  • 7
2

You might want to take a look at this package, it's not exactly what you are looking for but it is an alternative to sorting and organizing sections of your application including controller, migrations, models, views and others.

Nwidart's Laravel Modules for modular application development with Laravel,

It helps to organize your application into modules, and so, you can create migrations in each module, and since the modules are in separate folders it helps fix this issue someway.

Liam Hammett
  • 1,631
  • 14
  • 18
Ande Caleb
  • 1,163
  • 1
  • 14
  • 35