ramniksingh
5 months ago
631
3
Laravel

Access related tables with hasManyThrough non-linear relations

Posted 5 months ago by ramniksingh

I have 4 tables / models

1 . User -- Linked to CourseUser (one to many) hasMany

id, name, email .....

2 . CourseUser -- JunctionTable of course & user (many to many)

id,user_id, course_id, ....

3 . Course -- Linked to CourseUser (one to many) hasMany . Linked to video (one to many)

id,name,title.....

4 . Video -- Linked to Course

id,course_id, title, link.....

How can I access the videos as per Specific User?

Can I define hasThroughMany relation ?

Running the below in controller returns related arrays but video array shows empty items.

$courses = CourseUser::with('course.videos')->get();

dd($courses);

Please sign in or create an account to participate in this conversation.