i have four models -User -Question -Answer -Comment
the User model contains
public function questions(){
return $this->hasMany(Question::class);
}
public function answers(){
return $this->hasMany(Answer::class);
}
public function comments(){
return $this->hasMany(Comment::class);
}
the Question model contains
public function users(){
return $this->belongsTo(User::class,'user_id','id');
}
public function answers(){
return $this->hasMany(Answer::class);
}
public function comments(){
return $this->hasMany(Comment::class);
}
//the question table has user_id
the Answers model contains
public function questions(){
return $this->belongsTo(Question::class,'user_id','id');
}
public function users(){
return $this->belongsTo(User::class);
}
public function comments(){
return $this->hasMany(Comment::class);
}
//the answers table has user_id,question_id
the Comment model contains
public function questions(){
return $this->belongsTo(Question::class);
}
public function users(){
return $this->belongsTo(User::class);
}
public function answers(){
return $this->belongsTo(Answer::class);
}
//the answers table has user_id,question_id,answer_id
whenever access the Question model i can get access User,Answer,Comment related to that Question
but i want to get the details of the user that answers the question as well as the details of the user that commented on an answer.
this is my controller
public function show($slug){
$questionData = Question::with('users','answers','comments')->where('slug',$slug)->first();
return view('questions.single',compact('questionData'));
}
but wheneva i do
@foreach($questionData->answers as $answer)
$answer->user_id;
//it retuns only the user_id from the answers table
//how can i turn this to the username
@endforeach
i want to get the username of the user who answerd the question, as well as the username of the user that commented on the answer