Laravel organise migrations into folders

David Carr

Laravel Framework Tutorials

When a project grows the migrations folder can contain a lot of migration, ever wanted to desperate them into folders? turns out it's easy to so. All you need to do is tell Laravel where to read the migrations from.

In your AppServiceProvider.php boot call, you can call $this->loadMigrationsFrom() and give it a path of all the folder locations:

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

$this->loadMigrationsFrom($paths);

Now when you run

php artisan migrate

all folders will be scanned.

To migrate specific folders use --path for example for all migration in a folder called posts

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

or to make migration in a folder:

php artisan make:migration create_posts_table --path=/database/migrations/posts

 

Fathom Analytics $10 discount on your first invoice using this link

David Carr - Laravel Developer

Hi, I’m David Carr

A Senior Developer at Vivedia
I love to use the TALL stack (Tailwind CSS, Alpine.js, Laravel, and Laravel Livewire)

I enjoy writing tutorials and working on Open Source packages.

I also write books. I'm writing a new book Laravel Testing Cookbook, This book focuses on testing coving both PestPHP and PHPUnit.

Sponsor me on GitHub

Subscribe to my newsletter

Subscribe and get my books and product announcements.

Laravel Testing Cookbook

Help support the blog so that I can continue creating new content!

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2023 DC Blog. All code MIT license. All rights reserved.