Creating Custom Folders for Migration Files in Laravel Projects

This tutorial describes how to specify custom folders for migration files in your Laravel (6 or later) projects.
Provided by Leumas Naypoka / www.apphp.com

By default in Laravel you have all migration files are placed in one folder, that called database/migrations/

It's cool, but if your project has grown, you may found this folder includes a lot of files and it's not easy to support them. The best solution is to create sub-directories (according to your needs or per version) and create/put your migrations files in specific directory.

This is the standard way how we use migration files:

laravel standard migrations structure

Let's try to improve this. For example you may decide to separate migration files according to the version of your script, so the structure of your migration directory will look like this:

laravel optimized migrations structure

Looks much better, right? Instead of a long list of migration files.

So when you start new version, you need to create a new directory, name it with the next version number and all migrations, related to this version create only in new directory.

Is this enough? Not really.

But default Laravel waiting you place migration files directly in database/migrations/ directory, not in sub-directories. So we just need to inform Laravel from where it has to take migration files. Let's do it.

We have to open AppServiceProvider.php file, and add in the boot() method following code, that will tell to Laravel from where take migration files.

<?php
/*
|--------------------------------------------------------
|  Register custom migration paths
|  We specify sub-directory for each version, like:
|      - database/migrations/0.8.1/
|      - etc.
|  You may use DIRECTORY_SEPARATOR instead of /
|--------------------------------------------------------
*/
$this->loadMigrationsFrom([
  
database_path().'/migrations/0.8.1',
  
database_path().'/migrations/0.8.2',
  
database_path().'/migrations/0.8.3',
]); 
?>

Next time you will add a new directory, just remember to update this list of default directories.

Comments


Please post only comments related to the original tutorial. Be polite and helpful, do not spam or offend others.
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.

Quick Registration with: Facebook / Google