Laravel の make:migration コマンドで使用されるスタブを任意のものに変更します。コアのコードを拡張する形で実装します。
変更するスタブの準備
まず元のスタブを任意のディレクトリ(以下のサンプルでは resources/migration-stubs
)にコピーします。
$ cp vendor/laravel/framework/src/Illuminate/Database/Migrations/stubs/* resources/migration-stubs/
blank.stub
, create.stub
, update.stub
の3つのファイルがコピーされたと思います。今回はテーブル作成用のマイグレーションファイルの雛型となる create.stub
を編集します。
resources/migration-stubs/create.stub
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class DummyClass extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
try {
Schema::create('DummyTable', function (Blueprint $table) {
$table->bigIncrements('id');
$table->datetime('created_at');
$table->datetime('updated_at');
});
} catch (\Throwable $ex) {
$this->down();
throw $ex;
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('DummyTable');
}
}
変更点は
- 作成日時、更新日時をtimestamp型 → datetime型、かつNot NULL
- テーブル作成時に何らかの例外がスローされた場合、down()を実行
スタブの参照パスを変更する
コアのクラスを継承した以下2つのファイルを作成します。
app/Core/Database/Migrations/MigrationCreator.php
<?php
namespace App\Core\Database\Migrations;
use Illuminate\Database\Migrations\MigrationCreator as IlluminateMigrationCreator;
class MigrationCreator extends IlluminateMigrationCreator
{
/**
* Get the path to the stubs.
*
* @return string
*/
public function stubPath()
{
return resource_path('migration-stubs');
}
}
app/Core/Database/MigrationServiceProvider.php
<?php
namespace App\Core\Database;
use App\Core\Database\Migrations\MigrationCreator;
use Illuminate\Database\MigrationServiceProvider as IlluminateMigrationServiceProvider;
class MigrationServiceProvider extends IlluminateMigrationServiceProvider
{
/**
* Register the migration creator.
*
* @return void
*/
protected function registerCreator()
{
$this->app->singleton('migration.creator', function ($app) {
return new MigrationCreator($app['files']);
});
}
}
config/app.php
を編集します。以下、編集前後のdiff結果です。
@@ -143,7 +143,7 @@
Illuminate\Broadcasting\BroadcastServiceProvider::class,
Illuminate\Bus\BusServiceProvider::class,
Illuminate\Cache\CacheServiceProvider::class,
- Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
+ //Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
Illuminate\Cookie\CookieServiceProvider::class,
Illuminate\Database\DatabaseServiceProvider::class,
Illuminate\Encryption\EncryptionServiceProvider::class,
@@ -162,6 +162,11 @@
Illuminate\Validation\ValidationServiceProvider::class,
Illuminate\View\ViewServiceProvider::class,
+ // From ConsoleSupportServiceProvider
+ Illuminate\Foundation\Providers\ArtisanServiceProvider::class,
+ Illuminate\Foundation\Providers\ComposerServiceProvider::class,
+ App\Core\Database\MigrationServiceProvider::class,
+
/*
* Package Service Providers...
*/
結果
$ php artisan make:migration create_articles_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
try {
Schema::create('articles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->datetime('created_at');
$table->datetime('updated_at');
});
} catch (\Throwable $ex) {
$this->down();
throw $ex;
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}