Level 1
I solved it with this:
$students = User::where([ ['school_id', '=', $school->id], ['roles.Name', '=', 'Student'] ])
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->get();
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Guys, can you please help me build the query using eloquent? I'll explain how things are. I have users which have a role, and I want to filter only those that have role.Name == 'Student'. Also, each student can belong to many classes, so i have a classes table and users_classes where matching occurs. So far I have:
$students = User::where('school_id', '=', $school->id)->get();
How do i incorporate the role, and include functionality so that I can access the class like so:
@foreach($students as $student)
<a href="#" class="list-group-item">
{{ $student->first_name }} {{ $student->last_name }}
<span class="badge">
@foreach($student->ofClasses as $class)
{{ $class->name }}
@endforeach
</span>
</a>
@endforeach
I solved it with this:
$students = User::where([ ['school_id', '=', $school->id], ['roles.Name', '=', 'Student'] ])
->leftJoin('roles', 'roles.id', '=', 'users.role_id')
->get();
Please or to participate in this conversation.