Have you looked into withPivot() and wherePivot()?
https://laravel.com/docs/5.5/eloquent-relationships#many-to-many
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm working on school management system, I need to assign the student class and section here is the table structure.
Table: classes id, name
public function sections()
{
return $this->belongsToMany('App\Section');
}
public function students()
{
return $this->belongsToMany('App\Student');
}
Table: sections id, name
public function classes()
{
return $this->belongsToMany('App\Class');
}
Pivot Table: class_section class_id section_id
Table: Session (Current academic year)
id, year (17-18)
Table: Student id, name
public function classes()
{
return $this->belongsToMany('App\Class');
}
Now I have created a table: class_student with extra columns
class_student
class_id | student_id | section_id | session_id
1 3 2 1
While I'm editing student I need to fetch the student details with current session_id.
How to write the query to check where condition in the pivot table for an extra column?
If is there any possibilities with eloquent please let me know.
Thank you.
Please or to participate in this conversation.