laravel 5.4 显示数据中的关系
Relationships in laravel 5.4 ond display data
我有两个表,我想显示 :question_id.Question = question_id.QuestionOption
所在的所有字段。我不知道如何在 Laravel:
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('question_text');
$table->integer('points');
$table->integer('temps_reponse');
$table->integer('categories_id')->unsigned();
$table->foreign('categories_id')->references('id')->on('categories');
$table->integer('type_id')->unsigned();
$table->foreign('type_id')->references('id')->on('types');
$table->timestamps();
});
Schema::create('question_options', function (Blueprint $table) {
$table->increments('id');
$table->string('option_one');
$table->string('option_two');
$table->string('option_three');
$table->string('correcte');
$table->integer('question_id')->unsigned()->nullable();
$table->foreign('question_id')->references('id')->on('questions');
$table->timestamps();
});
我的 foreach 循环不起作用:
@foreach ($question_options as $question_option)
<tbody>
<tr>
<td>{{ $question_option->question->id }}</td>
<td>{{ $question_option->question->question_text }}</td>
<td>{{ $question_option->option_one }}</td>
<td>{{ $question_option->option_two }}</td>
<td>{{ $question_option->option_three }}</td>
<td>{{ $question_option->question->points }}</td>
</tr>
</tbody>
@endforeach
在模型中定义关系:
问题模型:
class Question extends Model {
public $table = 'questions';
public function options() {
return $this->hasMany(QuestionOption::class);
}
}
问题选项模型:
class QuestionOption extends Model {
public $table = 'questions_options';
public function question() {
return $this->belongsTo(Question::class);
}
}
从现在开始您可以访问选项问题选项:
$question = Question::with('options')->first();
在您看来:
@foreach($question->options as $question_option)
<tbody>
<tr>
<td>{{ $question_option->question->id }}</td>
<td>{{ $question_option->question->question_text }}</td>
<td>{{ $question_option->option_one }}</td>
<td>{{ $question_option->option_two }}</td>
<td>{{ $question_option->option_three }}</td>
<td>{{ $question_option->question->points }}</td>
</tr>
</tbody>
@endforeach
您可以直接从 $question
对象访问 question_text
而不是 $question_option->question->question_text
。
我有两个表,我想显示 :question_id.Question = question_id.QuestionOption
所在的所有字段。我不知道如何在 Laravel:
Schema::create('questions', function (Blueprint $table) {
$table->increments('id');
$table->string('question_text');
$table->integer('points');
$table->integer('temps_reponse');
$table->integer('categories_id')->unsigned();
$table->foreign('categories_id')->references('id')->on('categories');
$table->integer('type_id')->unsigned();
$table->foreign('type_id')->references('id')->on('types');
$table->timestamps();
});
Schema::create('question_options', function (Blueprint $table) {
$table->increments('id');
$table->string('option_one');
$table->string('option_two');
$table->string('option_three');
$table->string('correcte');
$table->integer('question_id')->unsigned()->nullable();
$table->foreign('question_id')->references('id')->on('questions');
$table->timestamps();
});
我的 foreach 循环不起作用:
@foreach ($question_options as $question_option)
<tbody>
<tr>
<td>{{ $question_option->question->id }}</td>
<td>{{ $question_option->question->question_text }}</td>
<td>{{ $question_option->option_one }}</td>
<td>{{ $question_option->option_two }}</td>
<td>{{ $question_option->option_three }}</td>
<td>{{ $question_option->question->points }}</td>
</tr>
</tbody>
@endforeach
在模型中定义关系:
问题模型:
class Question extends Model {
public $table = 'questions';
public function options() {
return $this->hasMany(QuestionOption::class);
}
}
问题选项模型:
class QuestionOption extends Model {
public $table = 'questions_options';
public function question() {
return $this->belongsTo(Question::class);
}
}
从现在开始您可以访问选项问题选项:
$question = Question::with('options')->first();
在您看来:
@foreach($question->options as $question_option)
<tbody>
<tr>
<td>{{ $question_option->question->id }}</td>
<td>{{ $question_option->question->question_text }}</td>
<td>{{ $question_option->option_one }}</td>
<td>{{ $question_option->option_two }}</td>
<td>{{ $question_option->option_three }}</td>
<td>{{ $question_option->question->points }}</td>
</tr>
</tbody>
@endforeach
您可以直接从 $question
对象访问 question_text
而不是 $question_option->question->question_text
。