What's the goal of this query ? To list all users only with quiz id ?
Sep 17, 2019
3
Level 5
How to Query GroupBy
I have users,quizzes and quiz_users table.
users table have id/name/email/password/timestamps
quizzes table have id/name/description/timestamps
quiz_users table have id/user_id/quiz_id
so i have 2 model
User.php and Quiz.php and quiz_users is pivot table, it contains the particular quiz assigned to particular user, user_id and quiz_id
Now i want to fetch all users groupBy quiz_id, how can i query it? i tried in this way
User::has('quizees')->groupBy('user_id')->paginate(10);
but it has error. Any help is highly appreciated.
Level 15
Why not try this in controller
$users = User::whereHas('Quizzes')->paginate();
In blade
@foreach($users as $user)
{{ $user->name }}
@foreach($user->Quizzes as $quiz)
{{ $quiz->name }}
@endforeach
@endforeach
Please or to participate in this conversation.