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
$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