在我使用的地方更新特定列值时遇到问题 php 内爆方法未正确更新
getting trouble when updating specific column values in where I used php implode method is not updating properly
我有一个 table questions
,其中 answerid
列的值类似于 1,2,3,4,5,6,7,8,9,10
。现在,当我要从 answers
table 中删除 answer
时,我想更新 questions
table。假设,我要从 answers
table 中删除 5
号码答案,并从 questions
table 中删除此 5
号码。但是当我这样做时 questions
table 正在像这样更新 - [{"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';
}
我有一个 table questions
,其中 answerid
列的值类似于 1,2,3,4,5,6,7,8,9,10
。现在,当我要从 answers
table 中删除 answer
时,我想更新 questions
table。假设,我要从 answers
table 中删除 5
号码答案,并从 questions
table 中删除此 5
号码。但是当我这样做时 questions
table 正在像这样更新 - [{"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';
}