Last active
March 28, 2024 17:10
-
-
Save msulaimanmisri/cbfd3a344bcd6c4178ad3aeb3082eaf7 to your computer and use it in GitHub Desktop.
Custom method to make Laravel Migration read the sub-folders inside migration folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Providers; | |
use Illuminate\Pagination\Paginator; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Support\ServiceProvider; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Register any application services. | |
*/ | |
public function register(): void | |
{ | |
// | |
} | |
/** | |
* Bootstrap any application services. | |
*/ | |
public function boot(): void | |
{ | |
Model::ShouldBeStrict(); | |
Paginator::useBootstrapFive(); | |
// For sub-folder migration files | |
$migrationsPath = database_path('migrations'); | |
$paths = $this->getAllMigrationFilesIncludingTheSubFolders($migrationsPath); | |
$this->loadMigrationsFrom($paths); | |
} | |
/** | |
* getAllMigrationFilesIncludingTheSubFolders | |
* | |
* @param string $directory | |
* @return array | |
*/ | |
function getAllMigrationFilesIncludingTheSubFolders(string $directory): array | |
{ | |
$directories = []; | |
$folders = array_diff(scandir($directory), ['.', '..']); | |
foreach ($folders as $folder) { | |
$path = $directory . DIRECTORY_SEPARATOR . $folder; | |
if (is_dir($path)) { | |
$directories[] = $path; | |
$directories = array_merge($directories, $this->getAllMigrationFilesIncludingTheSubFolders($path)); | |
} | |
} | |
return $directories; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In order for this to work, you need to run
php artisan make:migration --path=/database/migrations/your-sub-folder-name
Then run
php artisan migrate