Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ankur_g's avatar

Unable to add all the questions in the json in the database table in Laravel 9

I am parsing a json and inserting the records in the a database table, but only the qna array question are getting inserted and the rest of the questions are not.

The method which I have done is:

public function insertPersonaltyTestQuestionAnswer( request $request ) {
        $returnArray = array();
        $validator = Validator::make($request->all(), [
            'personality_test_id' => 'required|numeric|exists:tenant.persona_personality_tests,id',
            'qna'                 => 'required|array',
            'qnaOcean'            => 'required|array'
        ]);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors()], 401);
        }
        
        $msg = '';

        try {
            $PersonaPersonalityTest = PersonaPersonalityTest::find($request->personality_test_id);
            $PersonaPersonalityScore = new PersonaPersonalityScore();
            $PersonaPersonalityScore->personality_test_id = $PersonaPersonalityTest->id;
            $PersonaPersonalityScore->submit_count      = 0;
            $PersonaPersonalityScore->ext               = 0.00;
            $PersonaPersonalityScore->est               = 0.00;
            $PersonaPersonalityScore->agr               = 0.00;
            $PersonaPersonalityScore->csn               = 0.00;
            $PersonaPersonalityScore->opn               = 0.00;
            $PersonaPersonalityScore->ext_total         = 0;
            $PersonaPersonalityScore->est_total         = 0;
            $PersonaPersonalityScore->agr_total         = 0;
            $PersonaPersonalityScore->csn_total         = 0;
            $PersonaPersonalityScore->opn_total         = 0;
            $PersonaPersonalityScore->ext_percentage    = 0;
            $PersonaPersonalityScore->est_percentage    = 0;
            $PersonaPersonalityScore->agr_percentage    = 0;
            $PersonaPersonalityScore->csn_percentage    = 0;
            $PersonaPersonalityScore->opn_percentage    = 0;
            $PersonaPersonalityScore->iq_test_score     = null;
            $PersonaPersonalityScore->save();

            $maxcount = PersonaPersonalityScore::where( 'personality_test_id' , $request->personality_test_id )->count();
            $count = $maxcount;
            $answer_array = array();
            $questionarray = array();
            $answerarray = array();
            $csv = '/opt/lampp/htdocs/multitenant/public/files/iq_data.csv';
            $fp = fopen( $csv , 'w' );
            $personaqnaobject = new PersonaPersonalityTestsQna();
            foreach( $request->qnaOcean as $questions ) {
                
                $personaqnaobject->personality_score_id = $PersonaPersonalityScore->id;
                $personaqnaobject->question             = $questions['question'];
                $personaqnaobject->answer               = $questions['answer'];
                $personaqnaobject->submit_count         = $count;
                $personaqnaobject->request_type         = strtolower( $PersonaPersonalityTest->request_type );
                $personaqnaobject->save();
                
                if( !empty( $answer_array['text'] ) ) {
                    $answer_array['text'] .= ', '. $questions['answer'];
                }else {
                    $answer_array['text'] = $questions['answer'];
                }
            }

            foreach($request->qna as $questions){
                
                $personaqnaobject->question   = $questions['question'];
                $personaqnaobject->answer     = $questions['answer'];
                $personaqnaobject->save();
                $questionarray[]              = $questions['question'];
                $answerarray[]                = $questions['answer'];
            }

            $personascoreobject = PersonaPersonalityScore::find($PersonaPersonalityScore->id);
            $personascoreobject->personality_test_id = $request->personality_test_id;
            $personascoreobject->submit_count        = $count;
            
            $keyarray[] = "Timestamp";
            $valuearray[] = Carbon::now()->toDateTimeString();
            $t1 = array_merge($keyarray,$questionarray);
            $t2 = array_merge($valuearray,$answerarray);
            $row = array();
            array_push($row,$t1);
            array_push($row,$t2);
            
            for($i=0; $i < count($row); $i++) {
                fputcsv($fp, $row[$i]);
            }
            fclose( $fp );

            $data = array_chunk( explode( ',', $answer_array['text'] ), 10 );
            $avg_array        = [];
            $sum_array        = [];
            $percentage_array = [];
            $component_array  = [ 'ext' , 'est' , 'agr' , 'csn' , 'opn' ];
            
            $c = 0;
            foreach($data as $onedata) {
                $avg_array[$component_array[$c]]                        = array_sum( $onedata ) / 10;  
                $sum_array[$component_array[$c].'_total']               = array_sum( $onedata ); 
                $percentage_array[$component_array[$c].'_percentage']   = ( ( array_sum( $onedata ) / 50 ) * 100 );
                $c++;    
            }

            foreach( $avg_array as $k=>$v ) {
                $personascoreobject->$k = $v;
            }

            foreach( $sum_array as $k=>$v ) {
                $personascoreobject->$k = $v;
            }

            foreach( $percentage_array as $k=>$v ) {
                $personascoreobject->$k = $v;
            }
            
            $ch = curl_init();
            curl_setopt_array($ch, array(
                CURLOPT_URL => "http://44.193.201.129:8000/summarize",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_MAXREDIRS => 10,
                CURLOPT_TIMEOUT => 0,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_POSTFIELDS => array(''=> new \CURLFILE('/opt/lampp/htdocs/multitenant/public/files/iq_data.csv' , 'text/csv' , 'file')),
            ));
            $iq_response = curl_exec($ch);
            curl_close($ch);
            $personascoreobject->iq_test_score = $iq_response;
            $personascoreobject->save();
            
            $msg = 'Data Successfully Added.';
            $returnArray['status'] = "Success";
            $returnArray['msg'] = $msg;
            return response()->json($returnArray, 200);
        }
        catch( Exception $e ) {
            $returnArray['status'] = "Error";
            $returnArray['msg'] = $e;
            return response()->json($returnArray, 200);
        }

    }

The Json which I am receving from the request is:

{
  "personality_test_id": "43",
  "qna": [
    {
      "questionNo": 1,
      "question": "  I find it hard to imitate the behavior of other people.",
      "answer": "TRUE"
    },
    {
      "questionNo": 2,
      "question": "  My behavior is usually an expression of my true inner feelings, attitudes, and beliefs.",
      "answer": "TRUE"
    },
    {
      "questionNo": 3,
      "question": " At parties and social gatherings, I do not attempt to do or say things that others will like.",
      "answer": "TRUE"
    },
    {
      "questionNo": 4,
      "question": "  I can only argue for ideas which I already believe.",
      "answer": "TRUE"
    },
    {
      "questionNo": 5,
      "question": "  I can make impromptu speeches even on topics about which I have almost no information.",
      "answer": "TRUE"
    },
    {
      "questionNo": 6,
      "question": "  I guess I put on a show to impress or entertain people.",
      "answer": "TRUE"
    },
    {
      "questionNo": 7,
      "question": "  When I am uncertain how to act in a social situation, I look to the behavior of others for cues.",
      "answer": "TRUE"
    },
    {
      "questionNo": 8,
      "question": "  I would probably make a good actor.",
      "answer": "TRUE"
    },
    {
      "questionNo": 9,
      "question": "  I rarely seek the advice of my friends to choose movies, books, or music.",
      "answer": "TRUE"
    },
    {
      "questionNo": 10,
      "question": "  I sometimes appear to others to be experiencing deeper emotions than I actually am.",
      "answer": "TRUE"
    },
    {
      "questionNo": 11,
      "question": "  I laugh more when I watch a comedy with others than when alone.",
      "answer": "TRUE"
    },
    {
      "questionNo": 12,
      "question": "   In groups of people, I am rarely the center of attention.",
      "answer": "TRUE"
    },
    {
      "questionNo": 13,
      "question": "  In different situations and with different people, I often act like very different persons.",
      "answer": "TRUE"
    },
    {
      "questionNo": 14,
      "question": "  I am not particularly good at making other people like me.",
      "answer": "TRUE"
    },
    {
      "questionNo": 15,
      "question": "  Even if I am not enjoying myself, I often pretend to be having a good time. ",
      "answer": "TRUE"
    },
    {
      "questionNo": 16,
      "question": "  I'm not always the person I appear to be.",
      "answer": "TRUE"
    },
    {
      "questionNo": 17,
      "question": "  I would not change my opinions (or the way I do things) in order to please someone else or win their favor. ",
      "answer": "TRUE"
    },
    {
      "questionNo": 18,
      "question": "  I have considered being an entertainer.",
      "answer": "TRUE"
    },
    {
      "questionNo": 19,
      "question": "  In order to get along and be liked, I tend to be what people expect me to be rather than anything else. ",
      "answer": "TRUE"
    },
    {
      "questionNo": 20,
      "question": "  I have never been good at games like charades or improvisational acting. ",
      "answer": "TRUE"
    },
    {
      "questionNo": 21,
      "question": "  I have trouble changing my behavior to suit different people and different situations.",
      "answer": "TRUE"
    },
    {
      "questionNo": 22,
      "question": "   At a party, I let others keep the jokes and stories going.",
      "answer": "TRUE"
    },
    {
      "questionNo": 23,
      "question": "  I feel a bit awkward in company and do not show up quite as well as I should.",
      "answer": "TRUE"
    },
    {
      "questionNo": 24,
      "question": "  I can look anyone in the eye and tell a lie with a straight face (if for a right end).",
      "answer": "TRUE"
    },
    {
      "questionNo": 25,
      "question": "  I may deceive people by being friendly when I really dislike them.",
      "answer": "TRUE"
    },
    {
      "questionNo": 26,
      "question": "Select the statement that you agree with the most",
      "answer": "Children get into trouble because their patents punish them too much."
    },
    {
      "questionNo": 27,
      "question": "Select the statement that you agree with the most",
      "answer": "Many of the unhappy things in people's lives are partly due to bad luck."
    },
    {
      "questionNo": 28,
      "question": "Select the statement that you agree with the most",
      "answer": "One of the major reasons why we have wars is because people don't take enough interest in politics."
    },
    {
      "questionNo": 29,
      "question": "Select the statement that you agree with the most",
      "answer": "Unfortunately‚ an individual's worth often passes unrecognized no matter how hard he tries"
    },
    {
      "questionNo": 30,
      "question": "Select the statement that you agree with the most",
      "answer": "Most students don't realize the extent to which their grades are influenced by accidental happenings."
    },
    {
      "questionNo": 31,
      "question": "Select the statement that you agree with the most",
      "answer": "Without the right breaks one cannot be an effective leader."
    },
    {
      "questionNo": 32,
      "question": "Select the statement that you agree with the most",
      "answer": "People who can't get others to like them don't understand how to get along with others."
    },
    {
      "questionNo": 33,
      "question": "Select the statement that you agree with the most",
      "answer": "It is one's experiences in life which determine what they're like."
    },
    {
      "questionNo": 34,
      "question": "Select the statement that you agree with the most",
      "answer": "I have often found tlint what is going to happen will happen."
    },
    {
      "questionNo": 35,
      "question": "Select the statement that you agree with the most",
      "answer": "Many times exam questions tend to be so unrelated to course work that studying in really useless."
    },
    {
      "questionNo": 36,
      "question": "Select the statement that you agree with the most",
      "answer": "Becoming a success is a matter of hard work, luck has little or nothing to do with it."
    },
    {
      "questionNo": 37,
      "question": "Select the statement that you agree with the most",
      "answer": "This world is run by the few people in power, and there is not much the little guy can do about it."
    },
    {
      "questionNo": 38,
      "question": "Select the statement that you agree with the most",
      "answer": "When I make plans, I am almost certain that I can make them work."
    },
    {
      "questionNo": 39,
      "question": "Select the statement that you agree with the most",
      "answer": "There are certain people who are just no good."
    },
    {
      "questionNo": 40,
      "question": "Select the statement that you agree with the most",
      "answer": "In my case getting what I want has little or nothing to do with luck."
    },
    {
      "questionNo": 41,
      "question": "Select the statement that you agree with the most",
      "answer": "Getting people to do the right thing depends upon ability. Luck has little or nothing to do with it."
    },
    {
      "questionNo": 42,
      "question": "Select the statement that you agree with the most",
      "answer": "As far as world affairs are concerned, most of us are the victims of forces we can neither understand, nor control."
    },
    {
      "questionNo": 43,
      "question": "Select the statement that you agree with the most",
      "answer": "Most people don't realize the extent to which their lives are controlled by accidental happenings."
    },
    {
      "questionNo": 44,
      "question": "Select the statement that you agree with the most",
      "answer": "One should always be willing to admit mistakes."
    },
    {
      "questionNo": 45,
      "question": "Select the statement that you agree with the most",
      "answer": "How many friends you have depends upon how nice a person you are."
    },
    {
      "questionNo": 46,
      "question": "Select the statement that you agree with the most",
      "answer": "In the long run the bad things that happen to us are balanced by the good ones."
    },
    {
      "questionNo": 47,
      "question": "Select the statement that you agree with the most",
      "answer": "It is difficult for people to have much control over the things politicians do in office."
    },
    {
      "questionNo": 48,
      "question": "Select the statement that you agree with the most",
      "answer": "There is a direct connection between how hard 1 study and the grades I get."
    },
    {
      "questionNo": 49,
      "question": "Select the statement that you agree with the most",
      "answer": "A good leader makes it clear to everybody what their jobs are."
    },
    {
      "questionNo": 50,
      "question": "Select the statement that you agree with the most",
      "answer": "It is impossible for me to believe that chance or luck plays an important role in my life."
    },
    {
      "questionNo": 51,
      "question": "Select the statement that you agree with the most",
      "answer": "There's not much use in trying too hard to please people, if they like you, they like you."
    },
    {
      "questionNo": 52,
      "question": "Select the statement that you agree with the most",
      "answer": "There is too much emphasis on athletics in high school."
    },
    {
      "questionNo": 53,
      "question": "Select the statement that you agree with the most",
      "answer": "What happens to me is my own doing."
    },
    {
      "questionNo": 54,
      "question": "Select the statement that you agree with the most",
      "answer": "In the long run the people are responsible for bad government on a national as well as on a  local level."
    },
    {
      "questionNo": 55,
      "question": "It's not wise to tell your secrets.",
      "answer": "Disagree"
    },
    {
      "questionNo": 56,
      "question": "People see me as a natural leader.",
      "answer": "Disagree"
    },
    {
      "questionNo": 57,
      "question": "I like to get revenge on authorities.",
      "answer": "Disagree"
    },
    {
      "questionNo": 58,
      "question": "I like to use clever manipulation to get my way.",
      "answer": "Disagree"
    },
    {
      "questionNo": 59,
      "question": "I hate being the center of attention.",
      "answer": "Disagree"
    },
    {
      "questionNo": 60,
      "question": "I avoid dangerous situations.",
      "answer": "Disagree"
    },
    {
      "questionNo": 61,
      "question": "Whatever it takes, you must get the important people on your side.",
      "answer": "Disagree"
    },
    {
      "questionNo": 62,
      "question": "Many group activities tend to be dull without me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 63,
      "question": "Payback needs to be quick and nasty",
      "answer": "Disagree"
    },
    {
      "questionNo": 64,
      "question": "Avoid direct conflict with others because they may be useful in the future.",
      "answer": "Disagree"
    },
    {
      "questionNo": 65,
      "question": "I know that I am special because everyone keeps telling me so.",
      "answer": "Disagree"
    },
    {
      "questionNo": 66,
      "question": "People often say I'm out of control.",
      "answer": "Disagree"
    },
    {
      "questionNo": 67,
      "question": "It's wise to keep track of information that you can use against people later.",
      "answer": "Disagree"
    },
    {
      "questionNo": 68,
      "question": "I like to get acquainted with important people.",
      "answer": "Disagree"
    },
    {
      "questionNo": 69,
      "question": "It's true that I can be mean to others.",
      "answer": "Disagree"
    },
    {
      "questionNo": 70,
      "question": "You should wait for the right time to get back at people.",
      "answer": "Disagree"
    },
    {
      "questionNo": 71,
      "question": "I feel embarrassed if someone compliments me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 72,
      "question": "People who mess with me always regret it.",
      "answer": "Disagree"
    },
    {
      "questionNo": 73,
      "question": "There are things you should hide from other people because they don't need to know.",
      "answer": "Disagree"
    },
    {
      "questionNo": 74,
      "question": "I have been compared to famous people.",
      "answer": "Disagree"
    },
    {
      "questionNo": 75,
      "question": "I have never gotten into trouble with the law.",
      "answer": "Disagree"
    },
    {
      "questionNo": 76,
      "question": "Make sure your plans benefit you, not others.",
      "answer": "Disagree"
    },
    {
      "questionNo": 77,
      "question": "I am an average person.",
      "answer": "Disagree"
    },
    {
      "questionNo": 78,
      "question": "I enjoy having sex with people I hardly know",
      "answer": "Disagree"
    },
    {
      "questionNo": 79,
      "question": "Most people can be manipulated.",
      "answer": "Disagree"
    },
    {
      "questionNo": 80,
      "question": "I insist on getting the respect I deserve.",
      "answer": "Disagree"
    },
    {
      "questionNo": 81,
      "question": "I'll say anything to get what I want.",
      "answer": "Disagree"
    },
    {
      "questionNo": 82,
      "question": "I would prefer complex to simple problems.",
      "answer": "Disagree"
    },
    {
      "questionNo": 83,
      "question": "I like to have the responsibility of handling a situation that requires a lot of thinking.",
      "answer": "Disagree"
    },
    {
      "questionNo": 84,
      "question": "Thinking is not my idea of fun.",
      "answer": "Disagree"
    },
    {
      "questionNo": 85,
      "question": " I would rather do something that requires little thought than something that is sure to challenge my thinking abilities.",
      "answer": "Disagree"
    },
    {
      "questionNo": 86,
      "question": "I try to anticipate and avoid situations where there is likely a chance I will have to think in depth about something.",
      "answer": "Disagree"
    },
    {
      "questionNo": 87,
      "question": "I find satisfaction in deliberating hard and for long hours.",
      "answer": "Disagree"
    },
    {
      "questionNo": 88,
      "question": "I only think as hard as I have to.",
      "answer": "Disagree"
    },
    {
      "questionNo": 89,
      "question": "I prefer to think about small, daily projects to long-term ones.",
      "answer": "Disagree"
    },
    {
      "questionNo": 90,
      "question": "I like tasks that require little thought once I’ve learned them.",
      "answer": "Disagree"
    },
    {
      "questionNo": 91,
      "question": "The idea of relying on thought to make my way to the top appeals to me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 92,
      "question": "I really enjoy a task that involves coming up with new solutions to problems.",
      "answer": "Disagree"
    },
    {
      "questionNo": 93,
      "question": "Learning new ways to think doesn’t excite me very much.",
      "answer": "Disagree"
    },
    {
      "questionNo": 94,
      "question": "I prefer my life to be filled with puzzles that I must solve.",
      "answer": "Disagree"
    },
    {
      "questionNo": 95,
      "question": "The notion of thinking abstractly is appealing to me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 96,
      "question": "I would prefer a task that is intellectual, difficult, and important to one that is somewhat important but does not require much thought.",
      "answer": "Disagree"
    },
    {
      "questionNo": 97,
      "question": "I feel relief rather than satisfaction after completing a task that required a lot of mental effort.",
      "answer": "Disagree"
    },
    {
      "questionNo": 98,
      "question": "It’s enough for me that something gets the job done",
      "answer": "Disagree"
    },
    {
      "questionNo": 99,
      "question": " I don’t care how or why it works.",
      "answer": "Disagree"
    },
    {
      "questionNo": 100,
      "question": "I usually end up deliberating about issues even when they do not affect me personally.",
      "answer": "Disagree"
    },
    {
      "questionNo": 101,
      "question": "In most ways my life is close to my ideal.",
      "answer": "Disagree"
    },
    {
      "questionNo": 102,
      "question": "The conditions of my life are excellent.",
      "answer": "Disagree"
    },
    {
      "questionNo": 103,
      "question": "I am satisfied with my life.",
      "answer": "Disagree"
    },
    {
      "questionNo": 104,
      "question": "So far I have gotten the important things I want in life.",
      "answer": "Disagree"
    },
    {
      "questionNo": 105,
      "question": "If I could live my life over, I would change almost nothing.",
      "answer": "Disagree"
    },
    {
      "questionNo": 106,
      "question": "Over the past four weeks, how often have you felt positive",
      "answer": "Disagree"
    },
    {
      "questionNo": 107,
      "question": "Over the past four weeks, how often have you felt negative",
      "answer": "Disagree"
    },
    {
      "questionNo": 108,
      "question": "Over the past four weeks, how often have you felt good",
      "answer": "Disagree"
    },
    {
      "questionNo": 109,
      "question": "Over the past four weeks, how often have you felt bad",
      "answer": "Disagree"
    },
    {
      "questionNo": 110,
      "question": "Over the past four weeks, how often have you felt pleasant",
      "answer": "Disagree"
    },
    {
      "questionNo": 111,
      "question": "Over the past four weeks, how often have you felt unpleasant",
      "answer": "Disagree"
    },
    {
      "questionNo": 112,
      "question": "Over the past four weeks, how often have you felt happy",
      "answer": "Disagree"
    },
    {
      "questionNo": 113,
      "question": "Over the past four weeks, how often have you felt sad",
      "answer": "Disagree"
    },
    {
      "questionNo": 114,
      "question": "Over the past four weeks, how often have you felt afraid",
      "answer": "Disagree"
    },
    {
      "questionNo": 115,
      "question": "Over the past four weeks, how often have you felt joyful",
      "answer": "Disagree"
    },
    {
      "questionNo": 116,
      "question": "Over the past four weeks, how often have you felt angry",
      "answer": "Disagree"
    },
    {
      "questionNo": 117,
      "question": "Over the past four weeks, how often have you felt contented",
      "answer": "Disagree"
    },
    {
      "questionNo": 118,
      "question": "I worry about what other people will think of me even when I know it doesn't make any difference.",
      "answer": "Disagree"
    },
    {
      "questionNo": 119,
      "question": "I am unconcerned even if I know people are forming an unfavorable impression of me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 120,
      "question": "I am frequently afraid of other people noticing my shortcomings.",
      "answer": "Disagree"
    },
    {
      "questionNo": 121,
      "question": "I rarely worry about what kind of impression I am making on someone.",
      "answer": "Disagree"
    },
    {
      "questionNo": 122,
      "question": "I am afraid others will not approve of me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 123,
      "question": "I am afraid that people will find fault with me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 124,
      "question": "Other people's opinions of me do not bother me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 125,
      "question": "When I am talking to someone, I worry about what they may be thinking about me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 126,
      "question": "I am usually worried about what kind of impression I make.",
      "answer": "Disagree"
    },
    {
      "questionNo": 127,
      "question": "If I know someone is judging me, it has little effect on me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 128,
      "question": "Sometimes I think I am too concerned about what other people think of me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 129,
      "question": "I often worry that I will say or do the wrong things.",
      "answer": "Disagree"
    },
    {
      "questionNo": 130,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Finding something fun to do when I have no money",
      "answer": "Disagree"
    },
    {
      "questionNo": 131,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Helping other people cope with a difficult situation",
      "answer": "Disagree"
    },
    {
      "questionNo": 132,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Teaching someone how to do something",
      "answer": "Disagree"
    },
    {
      "questionNo": 133,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Maintaining a good balance between my work and my personal life",
      "answer": "Disagree"
    },
    {
      "questionNo": 134,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Understanding how to make myself happy",
      "answer": "Disagree"
    },
    {
      "questionNo": 135,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Being able to work through my personal problems in a healthy way",
      "answer": "Disagree"
    },
    {
      "questionNo": 136,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Thinking of new ways to help people",
      "answer": "Disagree"
    },
    {
      "questionNo": 137,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Choosing the best solution to a problem",
      "answer": "Disagree"
    },
    {
      "questionNo": 138,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Choosing the best solution to a problem",
      "answer": "Disagree"
    },
    {
      "questionNo": 139,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Planning a trip or event with friends that meets everyone’s  needs",
      "answer": "Disagree"
    },
    {
      "questionNo": 140,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Mediating a dispute or argument between two friends",
      "answer": "Disagree"
    },
    {
      "questionNo": 141,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Getting people to feel relaxed and at ease",
      "answer": "Disagree"
    },
    {
      "questionNo": 142,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Writing a non-fiction article for a newspaper, newsletter, or magazine",
      "answer": "Disagree"
    },
    {
      "questionNo": 143,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Writing a letter to the editor",
      "answer": "Disagree"
    },
    {
      "questionNo": 144,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Researching a topic using many different types of sources that may not be readily apparent",
      "answer": "Disagree"
    },
    {
      "questionNo": 145,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Debating a controversial topic from my own perspective",
      "answer": "Disagree"
    },
    {
      "questionNo": 146,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Responding to an issue in a context-appropriate way",
      "answer": "Disagree"
    },
    {
      "questionNo": 147,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Gathering the best possible assortment of articles or papers to support a specific point of view",
      "answer": "Disagree"
    },
    {
      "questionNo": 148,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Analyzing the themes in a good book",
      "answer": "Disagree"
    },
    {
      "questionNo": 149,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Figuring out how to integrate critiques and suggestions while revising a work",
      "answer": "Disagree"
    },
    {
      "questionNo": 150,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Being able to offer constructive feedback based on my own reading of a paper",
      "answer": "Disagree"
    },
    {
      "questionNo": 151,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Coming up with a new way to think about an old debate",
      "answer": "Disagree"
    },
    {
      "questionNo": 152,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Writing a poem",
      "answer": "Disagree"
    },
    {
      "questionNo": 153,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Making up lyrics to a funny song",
      "answer": "Disagree"
    },
    {
      "questionNo": 154,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Making up rhymes",
      "answer": "Disagree"
    },
    {
      "questionNo": 155,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Composing an original song",
      "answer": "Disagree"
    },
    {
      "questionNo": 156,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Learning how to play a musical instrument",
      "answer": "Disagree"
    },
    {
      "questionNo": 157,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Shooting a fun video to air on YouTube",
      "answer": "Disagree"
    },
    {
      "questionNo": 158,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Singing in harmony",
      "answer": "Disagree"
    },
    {
      "questionNo": 159,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Spontaneously creating lyrics to a rap song",
      "answer": "Disagree"
    },
    {
      "questionNo": 160,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Playing music in public",
      "answer": "Disagree"
    },
    {
      "questionNo": 161,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Acting in a play",
      "answer": "Disagree"
    },
    {
      "questionNo": 162,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Carving something out of wood or similar material",
      "answer": "Disagree"
    },
    {
      "questionNo": 163,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Figuring out how to fix a frozen or buggy computer",
      "answer": "Disagree"
    },
    {
      "questionNo": 164,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Writing a computer program",
      "answer": "Disagree"
    },
    {
      "questionNo": 165,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Solving math puzzles",
      "answer": "Disagree"
    },
    {
      "questionNo": 166,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Taking apart machines and figuring out how they work",
      "answer": "Disagree"
    },
    {
      "questionNo": 167,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Building something mechanical (like a robot)",
      "answer": "Disagree"
    },
    {
      "questionNo": 168,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Helping to carry out or design a scientific experiment",
      "answer": "Disagree"
    },
    {
      "questionNo": 169,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Solving an algebraic or geometric proof",
      "answer": "Disagree"
    },
    {
      "questionNo": 170,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Constructing something out of metal, stone, or similar material",
      "answer": "Disagree"
    },
    {
      "questionNo": 171,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Drawing a picture of something I’ve never actually seen (like an alien)",
      "answer": "Disagree"
    },
    {
      "questionNo": 172,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Sketching a person or object",
      "answer": "Disagree"
    },
    {
      "questionNo": 173,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Doodling/Drawing random or geometric designs",
      "answer": "Disagree"
    },
    {
      "questionNo": 174,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Making a scrapbook page out of my photographs",
      "answer": "Disagree"
    },
    {
      "questionNo": 175,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Taking a well-composed photograph using an interesting angle or approach",
      "answer": "Disagree"
    },
    {
      "questionNo": 176,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Making a sculpture or piece of pottery",
      "answer": "Disagree"
    },
    {
      "questionNo": 177,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Appreciating a beautiful painting",
      "answer": "Disagree"
    },
    {
      "questionNo": 178,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Coming up with my own interpretation of a classic work of art",
      "answer": "Disagree"
    },
    {
      "questionNo": 179,
      "question": "Compared to people of approximately your age and life experience, how creative would you rate yourself in regard to: Enjoying an art museum",
      "answer": "Disagree"
    },
    {
      "questionNo": 180,
      "question": "I lie in order to escape conflicts or disagreements with other people.",
      "answer": "Disagree"
    },
    {
      "questionNo": 181,
      "question": "I lie to hide the bad things I’ve done.",
      "answer": "Disagree"
    },
    {
      "questionNo": 182,
      "question": "I tell lies so I will not have confrontations with people.",
      "answer": "Disagree"
    },
    {
      "questionNo": 183,
      "question": "I lie in order to hide shameful things about myself. ",
      "answer": "Disagree"
    },
    {
      "questionNo": 184,
      "question": "I lie in order to prevent rifts in my relationships with others.",
      "answer": "Disagree"
    },
    {
      "questionNo": 185,
      "question": "I lie to stay out of arguments with people.",
      "answer": "Disagree"
    },
    {
      "questionNo": 186,
      "question": "I tell lies in order to spare another’s feelings.",
      "answer": "Disagree"
    },
    {
      "questionNo": 187,
      "question": "I lie to avoid disapproval from others.",
      "answer": "Disagree"
    },
    {
      "questionNo": 188,
      "question": "I lie in order to be friendly and cordial with others.",
      "answer": "Disagree"
    },
    {
      "questionNo": 189,
      "question": "I lie to others in order to conceal my misdeeds.",
      "answer": "Disagree"
    },
    {
      "questionNo": 190,
      "question": "I lie in order to punish people.",
      "answer": "Disagree"
    },
    {
      "questionNo": 191,
      "question": "I lie in order to take people down.",
      "answer": "Disagree"
    },
    {
      "questionNo": 192,
      "question": "I lie for revenge.",
      "answer": "Disagree"
    },
    {
      "questionNo": 193,
      "question": "I use lies to attack people I don’t like.",
      "answer": "Disagree"
    },
    {
      "questionNo": 194,
      "question": "I tell lies in order to hurt, annoy, or upset others.",
      "answer": "Disagree"
    },
    {
      "questionNo": 195,
      "question": " I lie because it is exciting.",
      "answer": "Disagree"
    },
    {
      "questionNo": 196,
      "question": "In uncertain times, I usually expect the best.",
      "answer": "Disagree"
    },
    {
      "questionNo": 197,
      "question": "It's easy for me to relax. ",
      "answer": "Disagree"
    },
    {
      "questionNo": 198,
      "question": "If something can go wrong for me, it will.",
      "answer": "Disagree"
    },
    {
      "questionNo": 199,
      "question": "I'm always optimistic about my future.",
      "answer": "Disagree"
    },
    {
      "questionNo": 200,
      "question": "I enjoy my friends a lot.",
      "answer": "Disagree"
    },
    {
      "questionNo": 201,
      "question": "It's important for me to keep busy.",
      "answer": "Disagree"
    },
    {
      "questionNo": 202,
      "question": "I hardly ever expect things to go my way.",
      "answer": "Disagree"
    },
    {
      "questionNo": 203,
      "question": "I don't get upset too easily.",
      "answer": "Disagree"
    },
    {
      "questionNo": 204,
      "question": "I rarely count on good things happening to me.",
      "answer": "Disagree"
    },
    {
      "questionNo": 205,
      "question": "Overall, I expect more good things to happen to me than bad.",
      "answer": "Disagree"
    }
  ],
  "qnaOcean": [
    {
      "questionNo": 1,
      "question": "I am the life of the party.",
      "answer": 2
    },
    {
      "questionNo": 2,
      "question": "I feel little concern for others.",
      "answer": 4
    },
    {
      "questionNo": 3,
      "question": "I am always prepared.",
      "answer": 2
    },
    {
      "questionNo": 4,
      "question": "I get stressed out easily.",
      "answer": 4
    },
    {
      "questionNo": 5,
      "question": "I have a rich vocabulary.",
      "answer": 2
    },
    {
      "questionNo": 6,
      "question": "I don't talk a lot.",
      "answer": 3
    },
    {
      "questionNo": 7,
      "question": "I am interested in people.",
      "answer": 3
    },
    {
      "questionNo": 8,
      "question": "I leave my belongings around.",
      "answer": 2
    },
    {
      "questionNo": 9,
      "question": "I am relaxed most of the time.",
      "answer": 3
    },
    {
      "questionNo": 10,
      "question": "I have difficulty understanding abstract ideas.",
      "answer": 3
    },
    {
      "questionNo": 11,
      "question": "I feel comfortable around people.",
      "answer": 3
    },
    {
      "questionNo": 12,
      "question": "I insult people.",
      "answer": 2
    },
    {
      "questionNo": 13,
      "question": "I pay attention to details.",
      "answer": 4
    },
    {
      "questionNo": 14,
      "question": "I worry about things.",
      "answer": 4
    },
    {
      "questionNo": 15,
      "question": "I have a vivid imagination.",
      "answer": 3
    },
    {
      "questionNo": 16,
      "question": "I keep in the background.",
      "answer": 3
    },
    {
      "questionNo": 17,
      "question": "I sympathize with others' feelings.",
      "answer": 4
    },
    {
      "questionNo": 18,
      "question": "I make a mess of things.",
      "answer": 2
    },
    {
      "questionNo": 19,
      "question": "I seldom feel blue.",
      "answer": 4
    },
    {
      "questionNo": 20,
      "question": "I am not interested in abstract ideas.",
      "answer": 2
    },
    {
      "questionNo": 21,
      "question": "I start conversations.",
      "answer": 3
    },
    {
      "questionNo": 22,
      "question": "I am not interested in other people's problems. \t",
      "answer": 2
    },
    {
      "questionNo": 23,
      "question": "I get chores done right away.",
      "answer": 3
    },
    {
      "questionNo": 24,
      "question": "I am easily disturbed.",
      "answer": 3
    },
    {
      "questionNo": 25,
      "question": "I have excellent ideas.",
      "answer": 3
    },
    {
      "questionNo": 26,
      "question": "I have little to say.",
      "answer": 2
    },
    {
      "questionNo": 27,
      "question": "I have a soft heart.",
      "answer": 4
    },
    {
      "questionNo": 28,
      "question": "I often forget to put things back in their proper place.",
      "answer": 2
    },
    {
      "questionNo": 29,
      "question": "I get upset easily.",
      "answer": 3
    },
    {
      "questionNo": 30,
      "question": "I do not have a good imagination.",
      "answer": 2
    },
    {
      "questionNo": 31,
      "question": "I talk to a lot of different people at parties.",
      "answer": 3
    },
    {
      "questionNo": 32,
      "question": "I like order.",
      "answer": 3
    },
    {
      "questionNo": 33,
      "question": "I change my mood a lot.",
      "answer": 4
    },
    {
      "questionNo": 34,
      "question": "I am quick to understand things.",
      "answer": 3
    },
    {
      "questionNo": 35,
      "question": "I don't like to draw attention to myself.",
      "answer": 4
    },
    {
      "questionNo": 36,
      "question": "I take time out for others.",
      "answer": 4
    },
    {
      "questionNo": 37,
      "question": "I shirk my duties.",
      "answer": 2
    },
    {
      "questionNo": 38,
      "question": "I have frequent mood swings.",
      "answer": 3
    },
    {
      "questionNo": 39,
      "question": "I use difficult words.",
      "answer": 2
    },
    {
      "questionNo": 40,
      "question": "I don't mind being the center of attention.",
      "answer": 2
    },
    {
      "questionNo": 41,
      "question": "I feel others' emotions.",
      "answer": 4
    },
    {
      "questionNo": 42,
      "question": "I follow a schedule.",
      "answer": 4
    },
    {
      "questionNo": 43,
      "question": "I get irritated easily.",
      "answer": 3
    },
    {
      "questionNo": 44,
      "question": "I spend time reflecting on things.",
      "answer": 3
    },
    {
      "questionNo": 45,
      "question": "I am quiet around strangers.",
      "answer": 4
    },
    {
      "questionNo": 46,
      "question": "I make people feel at ease.",
      "answer": 3
    },
    {
      "questionNo": 47,
      "question": "I am exacting in my work.",
      "answer": 3
    },
    {
      "questionNo": 48,
      "question": "I often feel blue.",
      "answer": 4
    },
    {
      "questionNo": 49,
      "question": "I am full of ideas.",
      "answer": 3
    },
    {
      "questionNo": 50,
      "question": "I am full of ideas.",
      "answer": 3
    }
  ]
}

Only the last question of the qna array is getting inserted and all the other questions are skipped.

Kindly state where am I doing it incorrectly.

0 likes
4 replies
tykus's avatar

It is difficult to work out what you're trying to achieve here; is the qna array being used somewhere?

ankur_g's avatar

@tykus The json contains a personality_test_id , qna and qnaOcean - two arrays, each containing 251 and 50 questions respectively.

I am trying to iterate the two arrays qna and qnaOcean and insert it in the table

tykus's avatar

@ankur_g again, I don't understand how you are using them; there are so many temporary variables being used, it is hard to understand what they all are doing.

Ultimately, does the array of quaestions (and answers) in qna and qnaOcean become separated into distinct rows in the database; or saved as JSON in a TEXT column?

ankur_g's avatar

@tykus Sorry I was unable to understand, I have made some changes as in my second for loop while iterating over the qna array in the following way:

foreach( $request->qnaOcean as $questions ) {
                $personaqnaobject = new PersonaPersonalityTestsQna();
                $personaqnaobject->personality_score_id = $PersonaPersonalityScore->id;
                $personaqnaobject->question             = $questions['question'];
                $personaqnaobject->answer               = $questions['answer'];
                $personaqnaobject->submit_count         = $count;
                $personaqnaobject->request_type         = strtolower( $PersonaPersonalityTest->request_type );
                $personaqnaobject->save();
                
                if( !empty( $answer_array['text'] ) ) {
                    $answer_array['text'] .= ', '. $questions['answer'];
                }else {
                    $answer_array['text'] = $questions['answer'];
                }
            }

            foreach($request->qna as $questions){
                $personaqnaobject = new PersonaPersonalityTestsQna();
                $personaqnaobject->personality_score_id = $PersonaPersonalityScore->id;
                $personaqnaobject->question         = $questions['question'];
                $personaqnaobject->answer           = $questions['answer'];
                $personaqnaobject->submit_count     = $count;
                $personaqnaobject->request_type     = strtolower( $PersonaPersonalityTest->request_type );
                $personaqnaobject->save();
                $questionarray[]                    = $questions['question'];
                $answerarray[]                      = $questions['answer'];
            }

Now it gets inserted, previously I was creating one object instance for which all the other questions were being overwritten, now two object instance is being created and the same code is repeated twice in the functions except the question and answers.

Please or to participate in this conversation.