eloquent orm select where 然后更新
eloquent orm select where and then update
我有一些代码
public function addvote($id,$varia){
$pollsres = Pollsres::where('poll_id', '=',$id)->where('poll_variant','=',$varia)->get();
// and poll_variant = ?',[$id,$varia])->get();
if($pollsres->isEmpty()){
$pollres = new Pollsres();
$pollres->poll_id = $id;
$pollres->poll_variant = $varia;
$pollres->poll_count = 1;
$pollres->save();
}else{
//return response()->json($pollsres);
var_dump($pollsres);
}
}
在数据库中我有 1 条记录 $id = 1 和 $varia = 1 但是我可以更新 $pollsres->poll_count 我有错误,如果我更新 $pollsres[0]->poll_count 我不仅更新所有记录为什么有 where 表达式(测试 db 中的 2 条记录)
我想检查我是否在数据库中有记录,如果没有,请添加新记录,如果有编辑此记录
因为你的table没有primary key
.
您需要为您 table 添加 auto increment
主键,因为 Laravel/Lumen does not support composite primary key (i.e: 2 primary keys or more).
然后问题就解决了
我有一些代码
public function addvote($id,$varia){
$pollsres = Pollsres::where('poll_id', '=',$id)->where('poll_variant','=',$varia)->get();
// and poll_variant = ?',[$id,$varia])->get();
if($pollsres->isEmpty()){
$pollres = new Pollsres();
$pollres->poll_id = $id;
$pollres->poll_variant = $varia;
$pollres->poll_count = 1;
$pollres->save();
}else{
//return response()->json($pollsres);
var_dump($pollsres);
}
}
在数据库中我有 1 条记录 $id = 1 和 $varia = 1 但是我可以更新 $pollsres->poll_count 我有错误,如果我更新 $pollsres[0]->poll_count 我不仅更新所有记录为什么有 where 表达式(测试 db 中的 2 条记录)
我想检查我是否在数据库中有记录,如果没有,请添加新记录,如果有编辑此记录
因为你的table没有primary key
.
您需要为您 table 添加 auto increment
主键,因为 Laravel/Lumen does not support composite primary key (i.e: 2 primary keys or more).
然后问题就解决了