Run Migration Files from Different Sub Directories in Laravel

Ariful Islam
2 min readAug 21, 2023

--

Programmers are required to tackle extensive projects and manage a multitude of migration files. Utilizing the migrations folder for execution can occasionally become troublesome.

To address this problem, a convenient solution involves categorizing migration files into distinct subdirectories according to modules or features. This article aims to elucidate the approach to attaining this objective, providing clarity on the process.

Here is my laravel project’s screenshot after making sub-directories inside database/migrations directory

Now it’s time to write some code inside app/Providers/AppServiceProvider.php for reaching to our goal. First we will have to read all sub-directories located inside database/migrations . After that we will load all migrations files under those sub directories.

Read all subdirectories using recursive getAllSubdirectoriesOptimized() method.

/**
* @param $dir
* @return array
*/
function getAllSubdirectoriesOptimized($dir)
{
$subdirectories = [];

$items = scandir($dir);

foreach ($items as $item) {
if ($item !== '.' && $item !== '..') {
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
$subdirectories[] = $path;
$subdirectoriesToAdd = $this->getAllSubdirectoriesOptimized($path);
foreach ($subdirectoriesToAdd as $subdirToAdd) {
$subdirectories[] = $subdirToAdd;
}
}
}
}

return $subdirectories;
}

This method will return all sub-directories as array like below

Array
(
[0] => D:\laragon\www\project\database\migrations\common
[1] => D:\laragon\www\project\database\migrations\common\module
[2] => D:\laragon\www\project\database\migrations\common\module\sub-module
[3] => D:\laragon\www\project\database\migrations\common\module\sub-module\folder1
[4] => D:\laragon\www\project\database\migrations\common\module\sub-module\folder2
[5] => D:\laragon\www\project\database\migrations\common\module\sub-module\folder3
[6] => D:\laragon\www\project\database\migrations\common\module\sub-module\folder4
)

After that we will call getAllSubdirectoriesOptimized() method from boot() method.

/**
* Bootstrap any application services.
*/
public function boot(): void
{
$migrationsPath = database_path('migrations');
$paths = $this->getAllSubdirectoriesOptimized($migrationsPath);

$this->loadMigrationsFrom($paths);
}

Finally Our app/Providers/AppServiceProvider.php will be

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}

/**
* Bootstrap any application services.
*/
public function boot(): void
{
$migrationsPath = database_path('migrations');
$paths = $this->getAllSubdirectoriesOptimized($migrationsPath);

$this->loadMigrationsFrom($paths);
}

/**
* @param $dir
* @return array
*/
function getAllSubdirectoriesOptimized($dir)
{
$subdirectories = [];

$items = scandir($dir);

foreach ($items as $item) {
if ($item !== '.' && $item !== '..') {
$path = $dir . DIRECTORY_SEPARATOR . $item;
if (is_dir($path)) {
$subdirectories[] = $path;
$subdirectoriesToAdd = $this->getAllSubdirectoriesOptimized($path);
foreach ($subdirectoriesToAdd as $subdirToAdd) {
$subdirectories[] = $subdirToAdd;
}
}
}
}

return $subdirectories;
}

}

Now run migration

php artisan migrate

I sometimes write blog for the community. It’s free work. If you think this blog helps you, then I request you to follow me in github and share the blog

--

--

Ariful Islam

Software Engineer. Working on PHP, Laravel, CI, CakePhp, API. Open Source contributor, Cloud Expert, Project Consultant; https://linkedin.com/in/arif98741