1

I am working on a QA app where i need to add multiple answers to a question as per requirement dynamically. for this i am sending an object which carry question and the answers like.

question={
    'question_text':'what is your name',
    'answers':[
            {'answer_text':'some answer','isCorrect':'1'},
            {'answer_text':'some answer','isCorrect':'1'},
            {'answer_text':'some answer'}    // answer may or may not have isCorrect key
        ]
    }

On server side I have two tables or migrations 1 for Question and 1 for answer. The answer table have three fields question_id, answer_text' andisCorrect'. question_id is the foreign key of question in answer table.

to store the objects what i am doing is

$question_text=Input::get('question_text');
$answers=Input::get('answers');

$question=new Question;
$question->question_text=$question_text;
$question->save();


foreach($answers as $ans){ 
   $answer=new Answer;
   $answer->question_id=$question->id;
   $answer->answer_text=$ans->answer_text;
   if($answer->isCorrect){
        $answer->is_correct=$ans->isCorrect;
   }
   else{
        $answer->is_correct=0;
   }
   $answer->save();
}

But while iterating there is an error

`production.ERROR: exception 'ErrorException' with message 'Trying to get property of non-object'`  

what wrong I am doing here. I am first time working on PHP. I am trying to iterate it like Javascript or Python way. Tell me how can i get the values of answers array object and store them.

8
  • What is the meaning of $ans.answer_text;? '.' is the text concatenation operator in PHP; this is not Java. You probably want ->. Commented May 23, 2014 at 11:29
  • 1
    Also, you shouldn't use $answer->isCorrect if you're not sure the property exists. Either use isset() to be sure, or just assign it based on is_correct: $answer->isCorrect = isset($ans->is_correct)? (int)$ans->is_correct : 0; Commented May 23, 2014 at 11:32
  • that was -> notation of PHP. i have corrected that. Thanx Iserni Commented May 23, 2014 at 11:40
  • Are you sure Sajid? Check your database values. Input::get('answers') should be returning an array. So $ans is an array, not an object. Commented May 23, 2014 at 11:46
  • Answer is an array not Object?? i am not getting you The Shift Exchange. Commented May 23, 2014 at 11:55

1 Answer 1

6

You dont seem to be referencing the array variables correctly. This should work:

foreach($answers as $ans){ 
   $answer=new Answer;
   $answer->question_id=$question->id;
   $answer->answer_text=$ans['answer_text'];
   $answer->is_correct = isset($ans['isCorrect']);
   $answer->save();
}

p.s. Im not sure about forEach - I'm suprised it works - but you should probably rename it to foreach to confirm to the normal standards

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

4 Comments

i have made some changes in question. please see again and consider if condition in your answer. Thank you
I've updated my answer. Also - if this doesnt work - the error should tell you what "line" the error occurs on - just tell us the exact code on that line.
Just fixed another issue I found. Use the latest code.
you are right The Shift Exchange. It is foreach not forEach. forEach is in javascript

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.