Jaskaran Singh Puri
Jaskaran Singh Puri

Reputation: 739

How to make laravel display grouped values

I'm developing a question answer app using laravel, I want to display all answers under each question for that particular question. The problem I'm facing is I'm unable to alter the view to display answer under the respective question, since there is no way to match ID's in laravel views!

My view code:

@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

Answer's model

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

Question's model:

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

Everything is display on the dashboard.blade.php, which runs on dashboard controller

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

Output I'm getting

Current Output

Ouput I want

Required output

Answer's table

enter image description here

How can I achive that output? I'm using Laravel 5.2

Upvotes: 1

Views: 196

Answers (1)

Amit Gupta
Amit Gupta

Reputation: 17658

Your query to fetch questions should be as:

$listQuestions= Question::where('people_id', '=', Auth::id())
                            ->with(['answers' => function($q) {
                                $q->where('people_id', '<>' ,Auth::id());
                             }])
                            ->get();

And your view file should be as:

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

Upvotes: 1

Related Questions