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

vincent15000's avatar

Polymorphism problem : courses having a sequence of different activities

Hello,

In a personal project (only for me), I have a courses table and each course is a sequence of activities. The activities are saved in different tables :

  • videos (youtube or uploaded video)
  • texts (written directly in the app or inserted from a URL)
  • exams (with various questions)
  • uploads (when students have to upload their work)
  • ...

So a course has one or many activities which are ordered to appear in a certain order to the students.

All activities are different, but all these activities have a title, a description, ... and only some fields are different according to the activity type.

I have the idea to do this that way using polymorphism. But inever really used polymorphism like this (only once or twice for little purposes). So it's quite new for me.

activities : title, description, order, course_id, activity_type, activity_id

Where activity_type contains for example App\Models\Video and activity_id contains the id the the video.

Here is my first question : is it a good idea to do so ?

Then in Laravel I can create the different models (Course, Activity, Video). It is possible with Laravel to create a Video model which inherits the Activity model and that this Video model retrieves the datas from the videos AND from the activities table ? I don't think that Laravel can do that, but perhaps I don't know how to do it (I know for example that Django does it).

So here is my second question : is it a good idea to create a Video model inheriting the Activity model ? Or is it better to create a simple Video model attached to the Activity model via a simple relationship ?

Well ... if you could help me to understand how to code this, it could be wonderful ;).

Thanks a lot.

Vincent

0 likes
0 replies

Please or to participate in this conversation.