Launching Drycomponents - Get Free HTML/React Components built using TailwindCSS

Database Migrations from Custom Directory on Laravel 5.3+

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.

Folder Structure

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.

Namespacing Folder

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 config/app.php file.

Migration Creation

Now, that everything is ready, lets create our migration file. I have migrations folder under code/Auth/.

php artisan make:migration create_sample_table --path=code/Auth/migrations

Above code will create a new migration file and place it under code/Auth/migrations folder.

Running Migration

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.

Happy Coding!

Would like to hear more from me?

Consider Signing up. No spam ever.