dimanche 30 octobre 2016

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=""></a></li>
            @foreach($listAnswers as $a)
                <ul>
                    <li></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();
}

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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire