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

Martialis89's avatar

Eloquent join

Hi guys. I have 3 tables and their related models, students, classrooms, school_years. These tables are linked as follow :

  • a school year have many classrooms
  • a classroom belongs to only one school year
  • a student can belong to many classroom (over years) but only one classroom per year
  • a classroom has many students
  • the pivot table used in classrooms - school_years relation carry a school_year field

Here's my problem. At the begining of each year, when a compose classrooms lists, i want to select students (new and older) who don't have a classroom for the current year (last created).

I tried to express that in an eloquent query using join. No success ! :-(

Can you help me please ? Thanks

0 likes
2 replies
Martialis89's avatar

Thanks for you reply !

If i'm not wrong your code will give me all students who don't have a classroom at all. This will exclude the older who have one or more classrooms from the previous school years.

My idea is to make two requests. One to getnew students and an other one to get old students with no classroom for current year.

But could i merge these two results in one collection ?

Please or to participate in this conversation.