雄辩orm选择位置,然后更新


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我有错误,如果我更新$polls res[0]->poll_count,我更新所有记录,不仅为什么有where表达式(测试数据库中的2个记录)

我会检查我是否在数据库中有记录,如果没有添加新的记录,如果编辑了这个记录

因为您的表没有primary key

您需要为表添加auto increment主键,因为Laravel/Lumen不支持复合主键(即:2个或更多主键)。

然后问题就会得到解决。