Reputation: 2901
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
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)
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
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 ?
Upvotes: 1
Views: 2810
Reputation: 23000
You need to pass an associated array instead of individual values:
$this->updateOrCreate(["user_id" => $id], ["role_name" => "newbie"]);
Upvotes: 3
Reputation: 33196
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"]
);
Upvotes: 3