0

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?

0

3 Answers 3

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

8 Comments

what is with('answers') here?
Thats how you Eager Load. Check the link to the official docs I provided.
okay, means answers in $question=QuestionBank::with('answers')->find($id); is the name of migration. isn't it?
'answers' is your relationship between question and answers table. You said you setup the relationship already.
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
|
1

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);
 }

6 Comments

it is working but returning a blank array for answers
have you declared HasMany in your QuestionBank Model?
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
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.
mysql> desc answer_choices; | Field | Type | Null | Key | Default | Extra | | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | question_id | int(10) unsigned | NO | MUL | NULL | | | answer_text | mediumtext | NO | | NULL | | | correct | tinyint(1) | YES | | NULL | | | created_at | timestamp | NO | | 0000-00-00 00:00:00 | | | updated_at | timestamp | NO | | 0000-00-00 00:00:00 | |
|

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.