1
Hi, using the Slim Framework for php, I’m trying to make a updateOrCreate
:
$eventos = Eventos::updateOrCreate(
["id" => $id, "id_usuario" => $id_usuario],
["nome" => $nome, "descricao" => $descricao]
);
The problem is that when you do the update he has to keep the id_usuario
and the id
, I thought I’d put the id_usuario
in the first "[]" but it gives the following error:
SQLSTATE[23000]: Integrity Constraint Violation: 1062 Duplicate entry '91' for key 'PRIMARY' (SQL: Insert into
eventos
(id
,id_usuario
,nome
) values (91, 1, Kashmir))
It gives Insert instead of update because, how can I fix it ?
Follow the Events table model:
<?php
class Eventos extends BaseModel{
protected $table = "eventos";
const UPDATED_AT = 'atualizado_em';
const CREATED_AT = 'criado_em';
protected $dateFormat = 'U';
protected $fillable = ["id", "nome", "descricao", "id_thumbnail", "local", "endereco", "place_id", "data_hora_inicio", "data_hora_fim", "responsavel", "telefone1", "telefone2", "email", "site", "nota", "status", "categoria", "facebook_page_url", "id_usuario", "preco"];
}
?>
Are you using Eloquent? It has the layout of this table
– novic
If he is creating it is because there is no event with the same
id
andid_usuario
that you’re going through. Thatid_usuario
has already been registered withid
different. Add (Before your code)\DB::listen(function($sql) {
 var_dump($sql);
 });
and check thequery
generated. So you will see the error.– Valdeir Psr
@Virgilionovic I am using the Eloquent yes, I will update the question with the model of the table
– Alan PS
Alan the
id
isauto_incremento
? if it is she can’t be in$fillable
– novic