代码之家  ›  专栏  ›  技术社区  ›  Rashed Hasan Vijayaragavan

更新我使用php内爆方法的特定列值时遇到问题,无法正确更新

  •  0
  • Rashed Hasan Vijayaragavan  · 技术社区  · 7 年前

    我有一张桌子 questions 在where列中 answerid 价值观就像 1,2,3,4,5,6,7,8,9,10 . 现在我想更新 问题 当我要删除一个 answer answers 表。假设,我要删除 5 数字答案来自 答案 表并删除此 号码从 问题 表。但当我这么做的时候 问题 表正在这样更新- [{"answerid":"1,2,3,4,6,7,8,9,10"}] 相反,我想更新如下 1,2,3,4,6,7,8,9,10 . 我试过这样的方法-

    public function deleteAnswer($id)
    {
        $questionId = DB::table('answers')->where('id', $id)->get(['questionid']);
        $qid = $questionId[0]->questionid;
        $questionAns = DB::table('questions')->where('id', $qid)->get(['answerid']);
    
        $ansId = explode(",",$questionAns);
        //dd($ansId);
    
    
        while ($aid = current($ansId)) {
            if ($aid == $id) {
                $offset =  key($ansId);
            }
            next($ansId);
        }
       unset($ansId[$offset]);
        $implodeAnsIds = implode(",",$ansId);
    
       DB::table('questions')->where('id', $qid)->update([
            'answerid' => $implodeAnsIds,
       ]);
    
       return 'done';
    
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Jerodev    7 年前

    这个 get() 方法将始终返回集合。但是,您需要一个单独的属性。

    解决办法是 pluck() 这个 answerid 列并选择第一行。这将只选择您需要的属性。

    $questionAns = DB::table('questions')->where('id', $qid)->pluck('answerid')->first();
    
    推荐文章