can poly relationships be linked?
Like User and Company Can have Conversation which will have Messages which will be linked with Conversation_Messages.
Questions for you. Will messages ever be without a conversation? Will messages ever belong to more than one conversation?
If the answer to both these questions is No then the polymorphic would be from Conversations to User/Company
So it would be
Conversation morphsToMany User, conversationable Converstaion morphsToMany Company, conversationable
Then a Conversation hasMany Messages.
Message hasOne Owner (user) and lastly
User belongsTo Company.
When the message is created (it is created by the user, not the company) it can be attached to the conversation which can be attached to a User or Company. Hope that helps
I tried to follow your example but got a bit lost with how I am trying to implement this. See if the following image helps explain what I am trying to achieve.
I could easily build each inbox with its own tables/models/etc but I would like to be able to optimize the app by using poly relationships.
Your picture seems fine but your schema seems incorrect. According to your picture the polymorphic would be conversationable.
conversationables
conversation_id
conversationable_id
conversationable_type (User or Company)
conversations
id
messages
id
user_id
conversation_id
content
users
id
company_id
companies
id
name
ok, so I have gotten this to work. Now the question is how do I use my Conversation methods (functions) on the Collection returned by the model from the poly relationship.
$user = Auth::user();
$convos = $user->conversations;
I am trying to integrate this L4 messaging system ( https://github.com/cmgmyr/laravel-messenger )
Like how to check if the conversation is unread etc.
https://github.com/cmgmyr/laravel-messenger/blob/v1/src/Cmgmyr/Messenger/Models/Thread.php
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community