Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Dave61's avatar

Many to many relationship

I need to select a student & find out which classes they have attended. Each student will attend multiple classes & each class will have multiple students.

I have a table 'classes' with 'classid' as primary key I have a table 'students' with 'studentid' as primary key

I also have an intermediate table 'students_classes' with 2 fields, 1 for 'studentid' and the other for 'classid' The table is populated, with 1 record for each each student in each class they did. All 3 tables have models & have controllers.

I can get the class list with this SQL code quite easily using this

select * from classes where classid in (select classid from students_classes where studentid = $studentid)

but after looking at as many Laravel articles on many-to-many relationships as I can find, I still can't figure out how to do it. This one in particular should get me there https://laravel.com/docs/5.2/eloquent-relationships#many-to-many but 6 hours later I still can't figure it out. Does anyone know of some more simple & clear example info on how to achieve this?

0 likes
3 replies
Cronix's avatar

I'd suggest following Eloquent conventions for your database design. It makes things easier if you do it how Eloquent expects it.

https://laravel.com/docs/5.6/eloquent#eloquent-model-conventions

So, you'd have 3 tables, with (at least) the following fields

classes (plural)
-id

students (plural)
-id

class_student (singular name of each table, in alphabetical order)
-class_id (singular table name, followed by underscore id (_id)
-student_id (singular table name, followed by underscore id (_id)

Laravel expects that the pivot table be a combination of the 2 tables it's using, using the singular name of each table, and in alphabetical order. These can of course be overridden, but it makes it more complicated, especially if you're just starting out.

Then you'd have 2 models: Class and Student. Both would have a many-to-many relationship. https://laravel.com/docs/5.6/eloquent-relationships#many-to-many

So in the Student model

public function classes() {
    return $this->belongsToMany(App\Class::class);
}

and the Class model would be the opposite

public function students() {
    return $this->belongsToMany(App\Student::class);
}

Then, to find the students who are in a class with id of 4.

$class = Class::with('students')->find(4);

@foreach ($class->students as $student)
    {{ $student->name }}
@endforeach

To find the classes that a student with id of 10 has

$student = Student::with('classes')->find(10);

@foreach ($student->classes as $class)
    {{ $class->title }}
@endforeach 
1 like
jlrdw's avatar

Also others have asked about students classes college classes and that kind of relationships before you could also try a general search.

https://laracasts.com/discuss?q=Students

That's just one, you could try classes teachers and other keywords.

Cronix's avatar

Don't know why it didn't dawn on me, but you should really call the "Class" class, Courses, since "Class" is a php reserved word.

Please or to participate in this conversation.