2017-04-08 4 views
1

Laravel 5を使用して、UserRoleの間にn:nの関係を持つ新しいプロジェクトを作成しています。続いphp artisan migrate:installを(働く): Laravelの移行:ピボットテーブルrole_userが存在しません

私は自分のデータベースを空にして、コマンドを入力し

[Illuminate\Database\QueryException] 
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'manon.role_user' doesn't exist 
(SQL: alter table `role_user` add `user_id` int not null, add `role_id` int not null) 

[PDOException] 
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'manon.role_user' doesn't exist 

私はthisチュートリアルに触発されました:php artisan migrateは、私は次のエラーを取得します。 Role.phpで

:User.phpで

public function users() 
{ 
    return $this->belongsToMany('User'); 
} 

public function roles() 
{ 
    return $this->belongsToMany('Role'); 
} 

ロールマイグレーション:

class CreateRolesTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('roles', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('name'); 
     }); 
    } 

    public function down() 
    { 
     Schema::dropIfExists('roles'); 
    } 
} 

ここ

は、コードのいくつかの簡単なビットでありますrole_user移行:

人の
class CreateRoleUserTable extends Migration 
{ 
    public function up() 
    { 
     Schema::table('role_user', function (Blueprint $table) { 
      $table->integer('user_id'); 
      $table->integer('role_id'); 
     }); 
    } 

    public function down() 
    { 
     Schema::dropIfExists('role_user'); 
    } 
} 

ユーザーの移行:あなたは、あなたがそれを変更すること、このテーブルではなく作成しているようtablecreateに変更されました:

class CreateUsersTable extends Migration 
{ 
    public function up() 
    { 
     Schema::create('users', function (Blueprint $table) { 
      $table->increments('id'); 
      $table->string('firstName'); 
      $table->string('lastName'); 
      $table->string('email')->unique(); 
      $table->string('password'); 
      $table->rememberToken(); 
      $table->timestamps(); 
     }); 
    } 

    public function down() 
    { 
     Schema::dropIfExists('users'); 
    } 
} 

答えて

2
Schema::table('role_user', function (Blueprint $table) { 

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

注変更する必要があります存在しないものは変更できません:)

移行はCreateRoleUserTableとも呼ばれます。したがって、移行は変更ではないことを示します。

関連する問題