2017-08-12 66 views
2

我想一些列添加created_atupdated_at欄目旁邊,例如:Laravel:數據庫 - 更多的 「日誌欄」

  • deleted_at
  • creator
  • updater
  • deleter

最後三個應該包含user.id

什麼是更好的方法?

  • A.將它放在migration
  • B.編輯Blueprint類?
+0

後做你想要它您的遷移? –

+0

@SazzadurRahman是的,我所有的項目 – Feralheart

回答

2

而不是直接編輯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'); 
    } 
} 
+0

的我試過,但我得到了'[Symfony的\分量\調試\異常\ FatalThrowableError] 上遷移 – Feralheart

+0

運行'作曲家dumpautoload'類「數據庫\ CustomBlueprint」未找到'然後它應該是固定的,我也更新了這個修復的答案 –

+0

謝謝,現在工作:) – Feralheart