Laravel eloquent query items of a kind
-
21-12-2019 - |
Question
Lets say i have a message thread (common thing in social networks) consisting of different items like
- Text Messages
- Voice Messages
- Images
Now it would be useful to have a query which queries all threadItems like
thread->threadItems->orderBy('created_at')->get()
to show the items on the client side i need to know of which kind they are, so i would need to attach the class to the models on the query.
Is there a way to do this in Eloquent? I thought about polymorphic relations because they include the class in the table. As the types above are all threadItems but have no common attributes i would split up the models on a database layer, so having one table per model seams reasonable.
Is there a way to do this elegantly?
Solution
Laravel 4.1 will introduce Many-To-Many Polymorphic Relationships. Then you can introduce a "parent" Message Model, which relates to your "child" text messages, voice messages and images in a Polymorphic matter?
Some related information can be found here: