Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I am working on a Q&A app in Laravel. Here, I have two migrations or database tables, one is question_bank and second is answer_choices. There is one to many relation between question and answers table. While retrieving a question, I want to retrieve all of the answers which are related with this question.

For this i wrote a method:

public function getQuestion($id){
    $question=QuestionBank::find($id);
    $answers=AnswerBank::where('question_id','=',$id)->get();
    $question->answers=$answers;
        return Response::json($question);
}

The answer_choices migration is :

class AnswerChoices extends Migration {
public function up()
{
Schema::create('answer_choices', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('question_id')->unsigned();
        $table->mediumtext('answer_text');
        $table->boolean('correct')->nullable();
        $table->foreign('question_id')->references('id')->on('question_bank');
    });
}

public function down()
{
    Schema::drop('answer_choices');
}
}

And the model is :

<?php

class AnswerBank extends \Eloquent {
protected $fillable = [];
protected $table = "answer_choices";
}

Question model is

<?php

class QuestionBank extends \Eloquent {
protected $fillable = [];
protected $table = "question_bank";
}

I expected i will get result as question.answers:[{},{},{}]
but on client side I am getting it like "question.answers":{} as a blank object. When I return only $answers, it shows all the answer objects in the array like [{},{},{]].

How can I get the answers objects as an array of objects in JavaScript?

share|improve this question

3 Answers 3

up vote 1 down vote accepted

Answer Given by Creator is totally fine just having a small mistake. Corect that and your code will be run.
Change this function

public function answers(){

    return $this->HasMany('AnswerBank','id','question_id');

 }

to

public function answers(){

    return $this->HasMany('AnswerBank','question_id','id');

}

Replace question_id with id and it will work.
And return question object with answers like this

public function getQuestion($id){  
    $answers=QuestionBank::with('answers')->find($id);
    return Response::json($answers);
}
share|improve this answer
    
Yes , thanx, it is working. –  Sajid Ahmad May 31 '14 at 13:22

I think you haven't declared any relation in model. It could be the reason for null output. Alright in you QuestionBank first declare a relation and then call

// in model QuestionBank 


public function answers(){

  return $this->HasMany('AnswerBank','id','question_id');

 } 

 // In AnswerBank Model use this

  public function question(){

  return  $this->BelongsTo('QuestionBank');

   }

Now you can get all answers to your question by Calling:

public function getQuestion($id){
  $answers=QuestionBank::with('answers')->find($id);
  return Response::json($answers);
 }
share|improve this answer
    
it is working but returning a blank array for answers –  Sajid Ahmad May 26 '14 at 13:16
    
have you declared HasMany in your QuestionBank Model? –  Creator May 26 '14 at 13:18
    
check i have updated my answer. –  Creator May 26 '14 at 13:23
    
Still it is returning empty array. but if i change return $this->HasMany('AnswerBank','id','question_id'); to return $this->belongsTo('AnswerBank','id','question_id'); it return a single answer as an object not array of objects or answers –  Sajid Ahmad May 27 '14 at 6:59
    
can you show you both table attribute here? according to you one question has many answers . So QuestionBank ->has many AnswerBank. IF this is the case above answer should be working. –  Creator May 27 '14 at 7:31

There is one to many relation between question and answers table. While retrieving a question, I want to retrieve all of the answers which are related with this question

Since you already have a relationship defined, you just need to Eager Load the relationship when you get the questions, and it will include the answers automatically

public function getQuestion($id){
    $question=QuestionBank::with('answers')->find($id);
    return Response::json($question);
}
share|improve this answer
    
what is with('answers') here? –  Sajid Ahmad May 26 '14 at 11:29
    
Thats how you Eager Load. Check the link to the official docs I provided. –  The Shift Exchange May 26 '14 at 11:31
    
okay, means answers in $question=QuestionBank::with('answers')->find($id); is the name of migration. isn't it? –  Sajid Ahmad May 26 '14 at 11:36
    
'answers' is your relationship between question and answers table. You said you setup the relationship already. –  The Shift Exchange May 26 '14 at 11:37
    
Schema::create('answer_choices', function (Blueprint $table) { $table->increments('id'); $table->integer('question_id')->unsigned(); $table->mediumtext('answer_text'); $table->boolean('correct')->nullable(); $table->foreign('question_id')->references('id')->on('question_bank'); }); This is the table or migration –  Sajid Ahmad May 26 '14 at 11:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.