If I understand your question, Your pivot table must contain an ID, and this ID will be the key to another model.
If this is not your question, explain the other better relationship so I can try to help you.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi folks!
I have many to many relationship for courses and students and the table is called course_student. However, in this pivot table, I have year_id that should have many to one relationship to another model. How should I structure this?
// Course model
public function students()
{
return $this->belongsToMany(Student::class, 'course_student', 'course_id', 'user_id')
->withPivot('')
->withTimestamps();
}
I'm not sure what should I put in withPivot function. Thanks in advance all!
Please or to participate in this conversation.