Join SQLSTATE[42S22]:未找到列:1054未知列Laravel
当我想将角色与用户耦合时,我收到此错误: SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“角色id”(SQL:插入Join SQLSTATE[42S22]:未找到列:1054未知列Laravel,join,laravel-5,roles,Join,Laravel 5,Roles,当我想将角色与用户耦合时,我收到此错误: SQLSTATE[42S22]:未找到列:“字段列表”中的1054未知列“角色id”(SQL:插入roles\u user(created\u at,roles\u id,updated\u at,user\u id)值(2015-09-12 09:37:35,22015-09-12:37:35,1)) 这是我的角色迁移: public function up() { Schema::create('roles',function (Bl
roles\u user
(created\u at
,roles\u id
,updated\u at
,user\u id
)值(2015-09-12 09:37:35,22015-09-12: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',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
首先,我不知道你为什么
$table->integer('roles_id')->unsigned();
在您的用户
表中?这种情况下,您希望每个用户有一个且只有一个角色。但这里的多对多关系表明,您需要一个用户能够扮演多个角色。所以我会把这条线去掉
然后,归属关系应该是一个角色模型,而不是模型表
class Role extends Model {
protected $table = 'roles';
}
class User extends Model {
protected $table = 'users';
public function roles()
{
return $this->belongsToMany('App\Role')->withTimestamps();
}
}