2

When I genarate a migration in Laravel, it automatically looks like this:

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();
    });
}

But I want to work more convenient, I want to let the database handle when I create and update a row instead of doing it myself everytime. I found a way to make this possible like this:

public function up()
{
    Schema::create('comments', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamp('created_at')->useCurrent();
        $table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
    });
}

But now everytime I make a migration, I manually have to change this and I don't think that's very convenient. Does anybody know how I can change the automatically generated migration in Laravel?

  • Eloquent manages these fields by default unless you're doing something weird with your models. What do you mean you have to do it yourself every time? – Bytewave Jul 11 '17 at 22:10
  • @Bytewave I want to be able to have those two lines of the created_at and the updated_at in a migration by default, but the defaul currently is $time->timestamps() – Youness Arsalane Jul 11 '17 at 22:43

3 Answers3

2

If you dig around in the source code you will find that:

  1. There's a file called create.stub in the framework.
  2. This file is used by the MigrationCreator to make migrations.

In principle you can do the following:

  1. Grab the built-in migration file and move it in another folder in your project (e.g. resouces/stubs probably) Note that you should copy the other stubs in that folder too even if you won't modify them.

  2. Then, override the default migration creator to use this file instead, this should work:

    class MyMigrationCreator extends MigrationCreator {
          protected function stubPath() {
                 return base_path("resources"); //Or something valid         
          }
    }
    

Then in your application service provider you can do:

$this->app->instance(MigrationCreator::class, resolve(MyMigrationCreator::class));

This will (hopefully) "trick" laravel into using your migration creator than the default one. However, creating tables is not something that happens so often to justify all this trouble.

Update: It should extend the migration creator.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • This will not work on Laravel 5.6 and maybe in couple of earlier minor versions. The reason os that MigrationServiceProvider is deferred and it creates instance of MigrationCreator class directly: $this->app->singleton('migration.creator', function ($app) { return new MigrationCreator($app['files']); }); The worst thing is that even if you redeclare $this->app->singleton('migration.creator', function ($app) {}) even in boot() - it will be overwritten because MigrationServiceProvider is deferred. I'm stil searching for the solution. =( – Swayok Jul 04 '18 at 13:55
  • @Swayok try using `$this->app->instance` instead of `singleton` and pre-resolve it yourself. Hopefully that will be flagged as resolved in the framework. – apokryfos Jul 04 '18 at 15:31
  • then it will be reloved every time service provider is called and this is not a way it should work because you do not need this class every time your app loads – Swayok Jul 05 '18 at 13:12
  • Well you can limit it to when you are running it in the CLI at least since that's when you need the migration creator. I know it's not a good solution but there doesn't seem to be an official way to change the stubs. – apokryfos Jul 05 '18 at 13:14
0

In Laravel 5.6 there seems to be no possibility to override MigrationCreator class because it is used directly in MigrationServiceProvider:

protected function registerCreator() {
    $this->app->singleton('migration.creator', function ($app) {
        return new MigrationCreator($app['files']);
    });
}

But you can hack MigrationServiceProvider next way:

  1. Create directory in your project root: overrides/Illuminate/Database
  2. Copy file vendor/laravel/framework/src/Illuminate/Database/MigrationServiceProvider.php into overrides/Illuminate/Database folder (note that class namespace will remain same)
  3. Modify overrides/Illuminate/Database/MigrationServiceProvider.php:

    protected function registerCreator() {
        $this->app->singleton('migration.creator', function ($app) {
             return new MyMigrationCreator($app['files']);
        });
    }
    
  4. Modify your composer.json ("Illuminate\\"):

    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/",
            "Illuminate\\": "overrides/Illuminate"
        }
    },
    
  5. Run composer dump-autoload. Composer will say

    `Warning: Ambiguous class resolution, "Illuminate\Database\MigrationServiceProvider" was found in both "$baseDir . '/overrides/Illuminate/Database/MigrationServiceProvider.php" and "/vendor/laravel/framework/src/Illuminate\Database\MigrationServiceProvider.php", the first will be used.`
    

Now your fake MigrationServiceProvider will be used instead of Laravel's but you won't be able to use original one. That's why we copied whole file.

Actually this way you can override MigrationCreator class but amount of code there is high and you actually will need to extend it with your MyMigrationCreator class and override several methods like stubPath() and maybe create(). But MigrationServiceProvider can be quite safely overriden because it contains several small methods that are unlikely will be changed until Laravel 6

Swayok
  • 436
  • 3
  • 12
0

Testet with Laravel 10.

Custom MigrationCreator:

<?php

namespace App\Services;

use Illuminate\Database\Migrations\MigrationCreator as Creator;

class MigrationCreator extends Creator
{
    // Custom Changes
}

Custom MigrateMakeCommand:

use Illuminate\Database\Console\Migrations\MigrateMakeCommand as Command;
use App\Services\MigrationCreator; # <- Custom Creator!
use Illuminate\Support\Composer;

class MigrateMakeCommand extends Command
{
    /**
     * Create a new migration install command instance.
     *
     * @param \App\Services\MigrationCreator $creator
     * @param \Illuminate\Support\Composer   $composer
     */
    public function __construct(MigrationCreator $creator, Composer $composer)
    {
        parent::__construct($creator, $composer);
    }
    
    // Custom Changes
}

ServiceProvider:

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Foundation\Application;
use App\Console\Commands\Migrations\MigrateMakeCommand; # <- Custom Command!
use App\Services\MigrationCreator; # <- Custom Creator!

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        $this->registerMigrateMakeCommand();
    }

    /**
     * Register a custom make:migration command.
     *
     * @return void
     */
    protected function registerMigrateMakeCommand(): void
    {
        $this->app->singleton(MigrateMakeCommand::class, function (Application $app) {
            $creator = new MigrationCreator($app['files'], $app->basePath('stubs'));

            $composer = $app['composer'];

            return new MigrateMakeCommand($creator, $composer);
        });
    }
}
Norman Huth
  • 519
  • 5
  • 16