Can one modify the templates created by artisan migrate command?

It’s doable in a fairly logical way, at least in Laravel 5

Subclass MigrationCreator and override getStubPath(), just copying the function over from the original class (it will use your subclass’s __DIR__)

<?php

namespace App\Database;

use Illuminate\Database\Migrations\MigrationCreator;

class AppMigrationCreator extends MigrationCreator
{
    public function getStubPath()
    {
        return __DIR__.'/stubs';
    }
}

Write a service provider to override migration.creator with your own subclass (it must be a deferred service provider, because you cannot override a deferred binding with an eager one):

<?php

namespace App\Database;

use Illuminate\Support\ServiceProvider;

class AppMigrationServiceProvider extends ServiceProvider
{
    protected $defer = true;

    public function register()
    {
        $this->app->singleton('migration.creator', function ($app) {
            return new AppMigrationCreator($app['files']);
        });
    }

    public function provides()
    {
        return ['migration.creator'];
    }
}

Add your service provider to config/app.php after the default ones.

Finally, copy vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs alongside your MigrationCreator subclass (in this example it would become app/Database/stubs) and edit the templates to your needs.

Keep the DummyClass and DummyTable names, as they are replaced with str_replace() to create the actual migrations files.

Leave a Comment