Php 使用雄辩的ORM将多维数组从post数组保存到数据库中
我的post数组中有多维数组,我想使用laravel的雄辩ORM将其保存到我的数据库中。代码如下:Php 使用雄辩的ORM将多维数组从post数组保存到数据库中,php,loops,laravel,Php,Loops,Laravel,我的post数组中有多维数组,我想使用laravel的雄辩ORM将其保存到我的数据库中。代码如下: $q = new Quiz; $data = Input::get(); for($i=0; $i < $query->no_questions; $i++) { $q->question = $data['question'][$i]; $q->answers = json_encode($data['answers'][
$q = new Quiz;
$data = Input::get();
for($i=0; $i < $query->no_questions; $i++)
{
$q->question = $data['question'][$i];
$q->answers = json_encode($data['answers'][$i]);
$q->correct_answer = $data['correctAnswer'][$i];
}
$q->save();
如何将$a、$b和$c附加到for循环中的$q[]中。以上只是我试图实现的一个示例
如果我的帖子不够清晰,请告诉我,提前谢谢。:) 您需要移动“创建新对象”并将零件保存到循环中。否则,您总是使用相同的模型实例进行操作,最后只保存这一个实例 这应该起作用:
$data = Input::get();
for($i=0; $i < $query->no_questions; $i++)
{
$q = new Quiz;
$q->question = $data['question'][$i];
$q->answers = json_encode($data['answers'][$i]);
$q->correct_answer = $data['correctAnswer'][$i];
$q->save();
}
$data=Input::get();
对于($i=0;$i<$query->no_questions;$i++)
{
$q=新测验;
$q->question=$data['question'][$i];
$q->answers=json_encode($data['answers'][$i]);
$q->correct_answer=$data['correctAnswer'][$i];
$q->save();
}
$data = Input::get();
for($i=0; $i < $query->no_questions; $i++)
{
$q = new Quiz;
$q->question = $data['question'][$i];
$q->answers = json_encode($data['answers'][$i]);
$q->correct_answer = $data['correctAnswer'][$i];
$q->save();
}