2016-10-16 149 views
1

我是Laravel和PHP的新手,所以我面對和錯誤我不知道如何解決。Laravel遷移繼承不起作用

基本的問題是,因爲很多表有primary:idcreated_by,updated_by列,我已經發現它是在我的遷移中繼承它們。

我使用PHP7

所以我有一個基類

class BaseMigration extends Migration { 

    public function up(string $tableName) { 
    Schema::create($tableName, function (Blueprint $table) { 
     $table->mediumIncrements('id'); 
     $table->primary('id'); 

     $table->unsignedMediumInteger('created_by')->references('id')->on('users'); 
     $table->unsignedMediumInteger('updated_by')->references('id')->on('users'); 
    }); 
    } 
} 

和延伸遷移

class CreateItemsTable extends BaseMigration { 

    public function up() { 
     parent::up('items'); 

     Schema::create('items', function (Blueprint $table) { 

      $table->string('name', 74); 
      $table->timestamps(); 
     }); 
    } 

    // ...... 
} 

然而php artisan migrate給了我這樣的:

[ErrorException] Declaration of CreateItemsTable::up() should be compatible with Illuminate\Database\Migrations\BaseMigration::up(string $tableName)

是因爲我正在跑雙up()

我缺少什麼?感謝您的善意幫助。

回答

2

我認爲你需要具有相同的函數簽名,所以通過string $tableName

class CreateItemsTable extends BaseMigration { 

    public function up(string $tableName) { 
     Schema::create('items', function (Blueprint $table) { 
      parent::up('items'); 

      $table->string('name', 74); 
      $table->timestamps(); 
     }); 
    } 

    // ...... 
}