2

I am working on online examination system on laravel when users get exam I want to receive some data from API and store in a database. Data will be something like this.

{
    "user_id": [
        "420"
    ],
    "test_id": [
        "12"
    ],
    "question_id": [
        "1",
        "2",
        "3",
        "4"
    ]
}

My Controller

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

$user_id     = $request->user_id;
$question_id = $request->question_id;
$test_id     = $request->test_id;

foreach ($data as $key)
{
    $user_id     = $key['user_id'];
    $test_id     = $key['test_id'];
    $question_id = $key['question_id'];

    $data1 = array(
        array('user_id'=> $user_id, 'question_id'=> $question_id, 'test_id'=> $test_id)
    );

    $fbres::insert($data1);
}
2
  • how is the database structure. how should questions store? is there any many to many relationship? Commented Aug 24, 2018 at 10:01
  • Possible duplicate of Store array in Laravel 5.6 Commented Aug 24, 2018 at 11:32

2 Answers 2

1

Encode the array using json_encode() php method. and no need to call insert into loop. It's time consuming and may blast your DB.

$fbres       = new Testapp;
$json        = $request->all();
$data        = $json;
$data        = $request->ques;
$user_id     = $request->user_id;
$question_id = $request->question_id;
$test_id     = $request->test_id;

$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'];
    foreach($value['question_id'] as $k => $v){
         $finalData = array('user_id'=> $usr_id, 'question_id'=> $v, 'test_id'=> $test_id);

         $fbres::insert($finalData);
    } 
}

Hope it will help you.

Sign up to request clarification or add additional context in comments.

10 Comments

Error "Undefined index: user_id" in Loop
Check in your array user_id is coming or not? If not then use isset() method on $key[''] and let me know is it helping or not?
When i use print_r($data) it return "Array ( [user_id] => Array ( [0] => 420 ) [test_id] => Array ( [0] => 12 ) [question_id] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 ) )"
@brij try this. I have edited the code and let me know
|
0

Convert arrays to json string using json_encode function and then store it

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

$data = $request->ques;

$user_id = $request->user_id;
$question_id = $request->question_id;
$test_id     = $request->test_id;

 $data1 = array(
        array('user_id'=> json_encode($user_id), 'question_id'=> json_encode($question_id), 'test_id'=> json_encode($test_id))
    );
    $fbres::insert($data1);

2 Comments

It's throw error "Undefined index: user_id" in foreach loop
why u r using foreach ? i have updated my answer, check now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.