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

MahmoudAdelAli's avatar

multi table realtionship

hello there , i have a some tables and i need to make a real relationship , and i study so hard for this relation but for now i cant understand how to make it with laravel , cause all examples about photos and tags :)

Courses

id          
| name          
| price          
| lectures     
| category    
| duration       
| moderator       
| quran_parts_start 
| quran_parts_end 
| status  
| rate      
| created_by   
| created_at        
| updated_at

And Table Teacher And Student Now to relate it i make a subscription table with the following columns

id
course_id
student_id
teacher_id
course_starts_at
course_end_at
created_at
updated_at
0 likes
3 replies
MahmoudAdelAli's avatar

i split this table to another tables like

student_subscriptions
id
course_id
student_id
starts_at
ends_at
and another created with updated
teacher_subscriptions
id
course_id
teacher_id
starts_at
ends_at
and another created with updated

Now I Have A courses and teachers and student and every one have a pivot table , and this picture of scheme

https://ibb.co/bbDrT29

how to make it with laravel ?

MahmoudAdelAli's avatar
MahmoudAdelAli
OP
Best Answer
Level 5

after many search i found the solution is , make it inside one table

id - item_id , bla_id, thing_id 

and the can get it with the following code

  return $this->belongsToMany(
            Courses::class,
            Subscriptions::class,
            'teacher_id',
            'student_id'
            )->withPivot('course_id');

Please or to participate in this conversation.