我想一些列添加created_at
和updated_at
欄目旁邊,例如:Laravel:數據庫 - 更多的 「日誌欄」
deleted_at
creator
updater
deleter
最後三個應該包含user.id
。
什麼是更好的方法?
- A.將它放在
migration
- B.編輯
Blueprint
類?
我想一些列添加created_at
和updated_at
欄目旁邊,例如:Laravel:數據庫 - 更多的 「日誌欄」
deleted_at
creator
updater
deleter
最後三個應該包含user.id
。
什麼是更好的方法?
migration
Blueprint
類?而不是直接編輯Laravel的核心Blueprint
類,您應該擴展它,並根據您的具體需求添加功能。這是一個例子,你可以做到這一點。
在database
目錄中創建一個CustomBlueprint
類,該目錄擴展了核心Blueprint
類,該類包含自定義列的定義。
<?php
namespace Database;
class CustomBlueprint extends \Illuminate\Database\Schema\Blueprint
{
public function customColumns() {
$this->integer('creator')->nullable();
$this->integer('updater')->nullable();
$this->integer('deleter')->nullable();
}
}
創建自定義藍圖運行
composer dumpautoload
然後創建您的遷移,如
php artisan make:migration create_tests_table
在你遷移文件使用customColumns
方法這樣
<?php
use Database\CustomBlueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Migrations\Migration;
class CreateTestsTable extends Migration
{
public function up()
{
$schema = DB::connection()->getSchemaBuilder();
$schema->blueprintResolver(function($table, $callback) {
return new CustomBlueprint($table, $callback);
});
$schema->create('tests', function($table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
$table->customColumns();
});
}
public function down()
{
Schema::dropIfExists('tests');
}
}
的我試過,但我得到了'[Symfony的\分量\調試\異常\ FatalThrowableError] 上遷移 – Feralheart
運行'作曲家dumpautoload'類「數據庫\ CustomBlueprint」未找到'然後它應該是固定的,我也更新了這個修復的答案 –
謝謝,現在工作:) – Feralheart
後做你想要它您的遷移? –
@SazzadurRahman是的,我所有的項目 – Feralheart