Reputation: 445
I am getting data from multiple tables in relation with each other. But in one of the relation I want to get userAnswers
records by where('user_id, $userID)
. What is correct syntax for it
public function survey_completed_show($userSurvey, $userID)
{
$userSurvey = UserSurvey::with('survey.questions.userAnswers')->find($userSurvey);
return view('surveys.conducted-answers', compact('userSurvey'));
}
I just want to get answers of the selected User, I am currently getting all answers by each user
Upvotes: 1
Views: 34
Reputation: 12218
you can use deep with
:
$userSurvey = UserSurvey::with(['survey'=>function($query)use( $userID){
$query->with(['questions'=>function($query)use( $userID){
$query->with(['userAnswers'=>function($query)use( $userID){
$query->where('user_id', $userID);
}]);
}]);
}])->find($userSurvey);
Upvotes: 1
Reputation: 1485
Assuming you only need to filter the relation and not the UserSurvey you might wanna try this
public function survey_completed_show($userSurvey, $userID)
{
$userSurvey = UserSurvey::with(['survey.questions.userAnswers' => function($q) use ($userID){
$q->where('user_id', $userID);
}])->find($userSurvey);
return view('surveys.conducted-answers', compact('userSurvey'));
}
Upvotes: 1