jeudi 23 février 2017

I want to fetch an questions by id and it has multiple answers

I have Question model that has many answers

class Question extends Model
{
    public function answers()
    {
        return $this->hasMany('App\Answer');
    }
}

also I have Answer model , in this each answer belong to a question.

class Answer extends Model
{
    //
    public function question()
    {
        return $this->belongsTo('App\Question');
    }

}

So what i am trying to do is I want to fetch a question by it's id and it should have all the answers that belong to this particular question and together I want to pass it to my view.

Here is what I have Done so far

 public function show($id)
    {
      $questions = Question::with('questions')
                           ->join('answers', 'answers.question_id' , '=' , $id)
                           ->select('questions.id', 
                                        'questions.ask_question',
                                        'answers.user_id',
                                        'answers.aanswer',
                                        'answers.created_at')
                           ->where('questions.id' , '=', 'answers.question_id')
                           ->get();



return view('pages.show', ['questions' => $questions , 'user' => Auth::user()]);
    }

I have tried several other way but that didn't work



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2luVATX
via IFTTT

Aucun commentaire:

Enregistrer un commentaire