将一个对象数组分配给Laravel中的另一个对象的键

将一个对象数组分配给Laravel中的另一个对象的键

问题描述:

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?

我正在使用Laravel的Q&amp; A应用程序。 在这里,我有两个迁移或数据库表,一个是question_bank,第二个是answer_choices。 问答表之间有一对多的关系。 在检索问题时,我想检索与此问题相关的所有答案。 p>

为此,我写了一个方法: p>

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

answer_choices迁移是: p>

  class AnswerChoices extends Migration {  
公共函数up()
 {
Schema :: create('answer_choices',function(Blueprint $ table){
 $ table-&gt; increments('id'); 
 $ table-&gt; integer('  question_id') - &gt; unsigned(); 
 $ table-&gt; mediumtext('answer_text'); 
 $ table-&gt; boolean('correct') - &gt; nullable(); 
 $ table-&gt  ; foreign('question_id') - &gt;引用('id') - &gt; on('question_bank'); 
}); 
} 
 
公共函数down()
 
 
 \ {
 
 
 \  drop('answer_choices'); 
} 
} 
  code>  pre> 
 
 

模型为: p>

 &lt  ;?php 
 
class AnswerBank扩展\ Eloquent {
protected $ fillable = []; 
protecte  d $ table =“answer_choices”; 
} 
  code>  pre> 
 
 

问题模型是 p>

 &lt;?php \ 问题解决方案扩展\ Eloquent {
protected $ fillable = []; 
protected $ table =“question_bank”; 
} 
  code>  pre> 
 
 

我预计我会得到 结果为 question.answers:[{},{},{}] code>
但是在客户端我得到它像“question.answers”:{} code >作为空白对象。 当我只返回 $ answers code>时,它会显示数组中的所有答案对象,如 [{},{},{]] code>。 p>

如何在JavaScript中将答案对象作为对象数组? p> div>

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

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

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