2016-08-22 63 views
-3

如何在遷移文件中放置多個外鍵和主鍵。在bellow我分享我的遷移文件代碼。如何在遷移中刪除表的外鍵和主鍵?

遷移文件

public function up() 
{ 
    Schema::create('role_user', function(Blueprint $table){ 
     $table->integer('role_id')->unsigned(); 
     $table->integer('user_id')->unsigned(); 


     $table->foreign('role_id') 
       ->references('id') 
       ->on('roles') 
       ->onDelete('cascade'); 

     $table->foreign('user_id') 
       ->references('id') 
       ->on('users') 
       ->onDelete('cascade'); 

     $table->primary(['role_id', 'user_id']); 

    }); 
} 
public function down() 
{ 
    Schema::drop('role_user'); 
    //how drop foreign and primary key here ? 
} 

回答

6

藍圖類提供dropForeigndropPrimary的方法允許你刪除外鍵約束和主鍵。

以下應該做的伎倆:

public function down() 
{ 
    Schema::table('role_user', function (Blueprint $table) { 
     $table->dropForeign('role_user_role_id_foreign'); 
     $table->dropForeign('role_user_user_id_foreign'); 
     $table->dropPrimary(); 
    }); 
} 
+0

喜@jedzej我可以除去在單個dropForeign功能等dropForeign([ 'foreign_one',「foreign_two])兩個外鍵; –

+0

你不能。無論如何,你爲什麼要這麼做?只需將一行保存在一個小的遷移文件中? –