Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用雄辩的ORM将多维数组从post数组保存到数据库中_Php_Loops_Laravel - Fatal编程技术网

Php 使用雄辩的ORM将多维数组从post数组保存到数据库中

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'][

我的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'][$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();
}