Database Migrations from Custom Directory on Laravel 5.3+February 20, 2017
While working on one of my projects, I wanted the database migration file to be placed in a separate folder rather having them all under the same
database/migrations folder. A quick browse through laravel documentation showed that you could achieve that from laravel 5.3+. Let’s see how to handle this.
I have created a new folder separately and placed my files there thus making my application depended only on laravel core files. So, whenever there is any update, with no or with minor changes my application can be updated to newer version easily.
I have namespaced my folder so that I can register my service providers and other files.
Registering Service Provider
I have created DatabaseServiceProvider under the
code/Auth/Providers/DatabaseServiceProvider. In order to register, lets add the code to
Now, that everything is ready, lets create our migration file. I have migrations folder under
php artisan make:migration create_sample_table --path=code/Auth/migrations
Above code will create a new migration file and place it under
Now in order to run a migration, you can do it in two ways. First, run migration by specifying path
php artisan migrate --path=code/Auth/migrations
Or run by adding
$this->loadMigrationsFrom() to your service provider like the following
loadMigrationsFrom takes one parameter $paths. You can add one location or multiple location by passing it as array. Laravel will resolve them and add them to migration paths. Now you can migrate by simple migrate command
php artisan migrate
That's all you need to load migrations from custom directory in laravel 5.3 and above.