如何在Laravel中存储JSON多数组数据库?

问题描述 投票:0回答:1

我在完成考试后在线考试门户工作我想在数据库中存储结果用户详细信息,所有数据都来自API。

数据看起来像

{
"user_id": "420",
"test_id": "2",
"question_id": [
    "12",
    "13"
],
"c_ans": [
    "a",
    "b"
],
"ans": [
    "a",
    "b"
]

}

我的Question_id数组数据存储成功,但不是c_ans和数组。

我的控制器

   $fbres       = new Testapp;
    $json        = $request->all();
    $data        = $json;

    $finalData = array();
    $blankArr = array();
    //Check for array 1-d or 2-d
    if(isset($data["user_id"])){
      array_push($blankArr, $data);
    }else{
     $blankArr = $data;
    } 

    foreach ($blankArr as $key => $value)
    {

        $usr_id     = $value['user_id']['0'];
        $test_id    = $value['test_id']['0'];
        $c_ans      = $value['c_ans']['0'];
        $ans        = $value['ans']['0'];



        foreach($value['question_id'] as $k => $v){
             $finalData = array('user_id'=> $usr_id, 'question_id'=> $v, 'test_id'=> $test_id,'c_ans' => $c_ans,'ans'=>$ans);
              $store= $fbres::insert($finalData);
        } 
    }

    if($store)
  {
    return response([
                'Success' => "1",
                'Message' => "Result Submitted Successfull..."
            ]);   
  }

答案存储qazxsw poi

php sql laravel
1个回答
1
投票

根据您的数据,您可以执行以下操作:

this is issue

问题是$ c_ans,$ ans和quest_id有一对一的映射。因此,逐个检索价值。

希望这会帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.