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?
If you dig around in the source code you will find that:
MigrationCreator
to make migrations.In principle you can do the following:
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.
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.