[英]updateOrCreate() gives Column not found: 1054 Unknown column '0' in 'where clause'
I'm trying to assign role to user when they attempt. 我正在尝试向用户分配角色。 But it gives me this error.
但这给了我这个错误。 I'm using
updateOrCreate()
because I want to use that method later to change user role 我使用
updateOrCreate()
是因为我想稍后使用该方法来更改用户角色
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select * from
roles
where (0
= user_id and1
= = and2
= 10) limit 1)SQLSTATE [42S22]:找不到列:1054“ where子句”中的未知列“ 0”(SQL:从
roles
中选择*其中(0
= user_id和1
= =和2
= 10)限制1)
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger("user_id")->index();
$table->string("role_name");
$table->foreign("user_id")->references("id")->on("users")->onDelete("cascade");
});
RegisterController RegisterController
protected function create(array $data)
{
$user = user::create([
'name' => $data['name'],
'email' => $data['email'],
"username" => $data["username"],
'password' => Hash::make($data['password']),
]);
if ($user) {
$model = new Role();
$model->assignNewbieRole($user->id);
}
return $user;
}
Role model 榜样
public function assignNewbieRole($id)
{
$this->updateOrCreate(["user_id","=",$id],["role_name","=","newbie"]);
}
How do I fix this ? 我该如何解决 ?
您需要传递关联的数组而不是单个值:
$this->updateOrCreate(["user_id" => $id], ["role_name" => "newbie"]);
The values have to be set using a associative array where the column name is the key and the value is the value you want to find/insert. 必须使用关联数组来设置值,其中列名是键,而值是要查找/插入的值。
$this->updateOrCreate(
["user_id" => $id],
["role_name" => "newbie"]
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.