php - updateOrCreate() 给出 Column not found: 1054 Unknown column '0' in 'where 子句'
问题描述
我正在尝试在用户尝试时为其分配角色。但它给了我这个错误。我正在使用updateOrCreate()
,因为我想稍后使用该方法来更改用户角色
SQLSTATE [42S22]:未找到列:1054 'where 子句'中的未知列'0'(SQL:select * from
roles
where (0
= user_id and1
= = and2
= 10) limit 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");
});
注册控制器
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;
}
好榜样
public function assignNewbieRole($id)
{
$this->updateOrCreate(["user_id","=",$id],["role_name","=","newbie"]);
}
我该如何解决 ?
解决方案
您需要传递关联的数组而不是单个值:
$this->updateOrCreate(["user_id" => $id], ["role_name" => "newbie"]);
推荐阅读
- prometheus - 将 influx stddev 翻译成 promQL
- c# - JSON字符串中特定位置的属性/值的JSON路径
- firebird - 选择在firebird中分配别名的字段名称
- javascript - 从 forEach 循环中的 if 语句返回字符串未定义
- operating-system - 我可以查看我在计算机上执行的操作的命令行等效项吗?
- f# - 从 F# 实例化 C# 9 记录
- javascript - Google Apps 脚本中的 Bitly API V4:尝试调用点击摘要时出错
- reactjs - React 请求 URL 会自行更改,具体取决于调用的位置。(axios 和 fetch 都有)
- dropbox - 重新初始化 Dropbox 无头 cli
- arrays - 用一个 malloc 分配一个字符串和字符串数组