我有一张桌子
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';
}