Laravel running migrations on "app/database/migrations" folder recursively
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Efficiently Running Laravel Migrations Recursively in Subfolders
Introduction: The Laravel framework provides us with a powerful tool for managing database schemas through migrations. However, when you have multiple subfolders within the migration folder and several tables to create or update, it becomes important to run these migrations recursively. In this blog post, we'll explore the best practices for achieving this in an efficient manner.
1. Understanding Laravel Migrations: Laravel's migrations are executed using Artisan commands. These commands help you manage database schemas through PHP classes called down methods. The default Laravel installation includes a single "migrations" directory, but as your application grows, it may be necessary to organize files by subfolders based on their related tasks or functionality.
2. Running Migrations in Subdirectories: By running the command `php artisan migrate`, Laravel will execute all migrations found within the "migrations" folder and its subdirectories. However, if a more granular approach is required, you can use the path option to specify the exact directory structure where migrations should be found.
3. Path Option for Migration Commands: The Artisan command for running migrations recursively is `php artisan migrate --path="app/database/migrations/*"`. This will execute all migration files within the "app/database/migrations" folder and its subdirectories (in this case, the folders 'relations' and 'translations').
4. Specifying a Specific Directory: To run migrations recursively in a specific directory or subdirectory, you can modify the path option accordingly. For instance, to execute only files from the 'translations' folder, use `php artisan migrate --path="app/database/migrations/translations/*"`.
5. Running Migrations Recursively on Specific Subdirectories: If you need to run migration commands for specific subfolders while leaving out others, you can add additional path options with the "--ignore" flag. For example, if you want to execute migrations from both 'relations' and 'translations', but exclude the 'create_user_table.php' file, you can use `php artisan migrate --path="app/database/migrations/relations/*,app/database/migrations/translations/*" --ignore="app/database/migrations/create_user_table.php"`.
6. Handling Duplicate Filenames: In case of duplicate filenames within a subdirectory, you can use the option `--path=-*` to run all migration files with the given prefix in that specific directory. For instance, you can run all migrations for each table by issuing the command `php artisan migrate --path="app/database/migrations/*_table.php"`.
7. Running Seeds After Migrations: Once your database schema is updated through the migration commands above, you may want to seed data into the newly created tables. Laravel offers a "seed" command that will help you populate your database with initial test or production data. You can run this command as follows: `php artisan db:seed --class="App\Database\Seeders\UserTableSeeder"`.
Conclusion: Running migrations recursively in Laravel is simple and efficient when organized properly. By understanding the path option and best practices, you'll be able to manage your database schema effectively without any hassle. Furthermore, incorporating these concepts into your project workflow will save time and improve overall productivity.