Answer the question
In order to leave comments, you need to log in
Why is the view receiving a field in json?
There are models: Questions and Blocks (categories of questions). One question can only be in 1 block. A block can contain multiple questions
Question.php
class Question extends Model
{
public function block() {
return $this->belongsTo('App\Block');
}
}
class Block extends Model
{
public function questions() {
return $this->hasMany('App\Block');
}
}
class QuestionController extends Controller
{
public function index() {
$questions = Question::all();
return view('question.index')->with('questions', $questions);
}
}
<td>{{ $question->block->name }}</td>
<td>{{ $question->block }}</td>
Answer the question
In order to leave comments, you need to log in
Try
Or do a eager load first:
class QuestionController extends Controller
{
public function index() {
$questions = Question::all()->load('block');
return view('question.index')->with('questions', $questions);
}
}
<td>{{ $question->block->name }}</td>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question