I have an existing app with a polymorphic relationship setup between the comments and the videos/images table (videos and images can have many comments):
videos table
- id
- title
images table
- id
- url
comments table
- id
- parent_id (video or image primary key)
- parent_type (video or image)
Now my client has requested to add an activityfeed to the app. The feed must pull in all the newly added videos and images ordered by rank with their comments.
I have added an activities table
activities table (new)
-id
-parent_id (video or image primary key)
-parent_type (video or image)
-rank
-metadata
The metadata column contains some denormalised data to display the feed so i don't have to pull in each and every video or image.
Note: above is a simplyfied version of my app. In reality i have more then only video / image types.
How would i setup Eloquent to fetch all the activities with all the related comments with the least possible queries ? I really really want to prevent to do queries in a foreachloop.
An other database scheme for the activities table is possible.
Activities::with('parent.comments'). - Walid Ammar