Support the ongoing development of Laravel.io →
posted 9 years ago
Eloquent
Last updated 2 years ago.
0

can poly relationships be linked?

Like User and Company Can have Conversation which will have Messages which will be linked with Conversation_Messages.

0

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

0

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.

diagram

0

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.

0

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
Last updated 9 years ago.
0

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

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.