Click here to Skip to main content
15,900,378 members
Articles / Programming Languages / PHP
Tip/Trick

How to Create Custom Migration Directories in Laravel Projects

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
25 Dec 2020CPOL1 min read 4.2K   1  
Create custom directory structure for migration files in your Laravel projects
This article describes how to create custom directory structure for migration files in your Laravel (6 or later) projects. You could organize your migration folder structure in a better way, especially if you have a lot of migration files.

Introduction

This tutorial describes how to specify custom folders for migration files in your Laravel (6 or later) projects.

What Is the Problem with Migration Directory

As you know, in Laravel, by default, all your migration files are placed in one directory called database/migrations/.

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

This is the standard way of how we use migration files in Laravel projects:

How to Implement Migration Sub-Directories

Let's try to improve this feature. 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:

Now it looks much better, right? Instead of a long list of migration files.

So when you start a 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 Laravel from where to take migration files.

PHP
<?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 when you will need to add a new directory, just remember to update this list of default directories.

History

  • 25th December, 2020: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO ApPHP
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --