如何使 laravel 显示分组值
How to make laravel display grouped values
我正在使用 laravel 开发问答应用程序,我想在每个问题下显示该特定问题的所有答案。我面临的问题是我无法更改视图以显示相应问题下的答案,因为无法在 laravel 视图中匹配 ID!
我的查看代码:
@section('list-questions-answers')
<ul>
@foreach($listQuestions as $q)
<li><a href="{{url('/questions/'.$q->question_id)}}">{{ $q->question }}</a></li>
@foreach($listAnswers as $a)
<ul>
<li>{{ $a->answer }}</li>
</ul>
@endforeach
@endforeach
</ul>
@endsection
答案的模型
public function User()
{
return $this->belongsTo('App\User');
}
public function questions()
{
return $this->belongsTo('App\questions','answer_id');
}
public function scopefetchAnswers($query)
{
return $query->select('answer')->where('people_id','<>',Auth::id())->get();
}
问题的型号:
protected $table='questions';
protected $primaryKey='question_id';
protected $fillable = [
'question', 'created_at','details'
];
//protected $hidden=['question_id'];
public function setUpdatedAtAttribute($value) {}
public function User() {
return $this->belongsTo('App\User');
}
public function answers()
{
return $this->hasMany('App\answers','question_id');
}
public function scopefetchQuestions($query) {
return $query->select('question','question_id')->where('people_id','=',Auth::id())->get();
}
一切都显示在 dashboard.blade.php 上,它在仪表板控制器上运行
class Dashboard extends Controller
{
public function index(){
$listQuestions=questions::latest('created_at')->fetchQuestions();
//$listAnswers=answers::groupBy('question_id')->fetchAnswers();
$listAnswers=answers::fetchAnswers();
return view('forms.question',compact('listQuestions','listAnswers'));
}
}
我得到的输出
我想要的输出
答案table
我怎样才能得到那个输出?我正在使用 Laravel 5.2
您获取问题的查询应为:
$listQuestions= Question::where('people_id', '=', Auth::id())
->with(['answers' => function($q) {
$q->where('people_id', '<>' ,Auth::id());
}])
->get();
您的视图文件应为:
section('list-questions-answers')
<ul>
@foreach($listQuestions as $q)
<li><a href="{{url('/questions/'.$q->question_id)}}">{{ $q->question }}</a></li>
@foreach($q->answers as $a)
<ul>
<li>{{ $a->answer }}</li>
</ul>
@endforeach
@endforeach
</ul>
@endsection
我正在使用 laravel 开发问答应用程序,我想在每个问题下显示该特定问题的所有答案。我面临的问题是我无法更改视图以显示相应问题下的答案,因为无法在 laravel 视图中匹配 ID!
我的查看代码:
@section('list-questions-answers')
<ul>
@foreach($listQuestions as $q)
<li><a href="{{url('/questions/'.$q->question_id)}}">{{ $q->question }}</a></li>
@foreach($listAnswers as $a)
<ul>
<li>{{ $a->answer }}</li>
</ul>
@endforeach
@endforeach
</ul>
@endsection
答案的模型
public function User()
{
return $this->belongsTo('App\User');
}
public function questions()
{
return $this->belongsTo('App\questions','answer_id');
}
public function scopefetchAnswers($query)
{
return $query->select('answer')->where('people_id','<>',Auth::id())->get();
}
问题的型号:
protected $table='questions';
protected $primaryKey='question_id';
protected $fillable = [
'question', 'created_at','details'
];
//protected $hidden=['question_id'];
public function setUpdatedAtAttribute($value) {}
public function User() {
return $this->belongsTo('App\User');
}
public function answers()
{
return $this->hasMany('App\answers','question_id');
}
public function scopefetchQuestions($query) {
return $query->select('question','question_id')->where('people_id','=',Auth::id())->get();
}
一切都显示在 dashboard.blade.php 上,它在仪表板控制器上运行
class Dashboard extends Controller
{
public function index(){
$listQuestions=questions::latest('created_at')->fetchQuestions();
//$listAnswers=answers::groupBy('question_id')->fetchAnswers();
$listAnswers=answers::fetchAnswers();
return view('forms.question',compact('listQuestions','listAnswers'));
}
}
我得到的输出
我想要的输出
答案table
我怎样才能得到那个输出?我正在使用 Laravel 5.2
您获取问题的查询应为:
$listQuestions= Question::where('people_id', '=', Auth::id())
->with(['answers' => function($q) {
$q->where('people_id', '<>' ,Auth::id());
}])
->get();
您的视图文件应为:
section('list-questions-answers')
<ul>
@foreach($listQuestions as $q)
<li><a href="{{url('/questions/'.$q->question_id)}}">{{ $q->question }}</a></li>
@foreach($q->answers as $a)
<ul>
<li>{{ $a->answer }}</li>
</ul>
@endforeach
@endforeach
</ul>
@endsection