简体   繁体   English

updateOrCreate()给出未找到的列:1054“ where子句”中的未知列“ 0”

[英]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 and 1 = = and 2 = 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.

相关问题 1054 where子句中的未知列? - 1054 Unknown column in where clause? 找不到列:1054“ where子句”中的未知列“ id”-Laravel - Column not found: 1054 Unknown column 'id' in 'where clause' - Laravel 未找到列:1054“ where子句”中的未知列“ id” - Column not found: 1054 Unknown column 'id' in 'where clause' 未找到列:1054 'where 子句'中的未知列'id' Laravel 5.6 - Column not found: 1054 Unknown column 'id' in 'where clause' Laravel 5.6 “ where子句”中的未知列“ x” [1054] - Unknown column 'x' in 'where clause' [1054] PDO#1054“ where”子句中的未知列“ n” - PDO #1054 Unknown column 'n' in 'where clause 错误:SQLSTATE[42S22]:未找到列:1054 'where 子句'中的未知列 '0' - ERROR: SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'where clause' SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'Users.email' - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Users.email' in 'where clause' SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'名称' - SQLSTATE[42S22]: Column not found: 1054 Unknown column ' name' in 'where clause' SQLSTATE [42S22]:找不到列:1054 Yii 1.1中“ where子句”中的未知列“ registration” - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'registration' in 'where clause' in Yii 1.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM