【发布时间】:2015-12-08 20:01:33
【问题描述】:
当我想将角色耦合到用户时收到此错误:
SQLSTATE[42S22]:找不到列:1054 '字段列表'中的未知列'roles_id'(SQL:插入
roles_user(created_at,roles_id,updated_at,user_id)值( 2015-09-12 09:37:35, 2, 2015-09-12 09:37:35, 1))
这是我的角色迁移:
public function up()
{
Schema::create('roles',function (Blueprint $table){
$table->increments('id');
$table->string('name')->unique();
$table->timestamps();
});
DB::table('roles')->insert(array(
array('name' => 'user'),
array('name' => 'admin'),
));
}
这是我的用户迁移:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->integer('roles_id')->unsigned();
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
这是我的数据透视表:
public function up()
{
Schema::create('roles_user',function(Blueprint $table)
{
$table->increments('id');
$table->integer('role_id')->unsigned()->index();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
}
在我的角色模型中,我是说:
public function roles()
{
return $this->belongsToMany('App\roles')->withTimestamps();
} here
【问题讨论】: