Reputation: 155
I have 3 models: Events
, groups
and users
.
Relations:
Events - Users - many to many. Groups - Users - many to many.
There will be one chat withing group and one inside event. I am going to implement chats between two or more users. What is the best way to design it? I created Chat model and many to many relation between chat and users.
First idea: The members of group and event chats will be stored in respectively event_users
and group_users
tables. Only chats between users will be stored in chat_users
.
Second idea: chat_users
will be synchronized with event_users
and group_users
tables. The advantage is separated logic to manage chats and there will be not many complicated queries.
Upvotes: 3
Views: 3034
Reputation: 5603
User can participate to an event and event can have many users who participate to that event, in this specific case we need to use as Many to Many relation Laravel One to Many Relation
event_user
User can be member of many group and a group can avec many member, in this case we need a Many to Many relation Laravel Many to Many Relation
group_user
chat can be related to an event or a group, so we save the type of the chat witch can be either Event or Group and the corresponding ID of that event or group. so if we have a particular chat we can retreive all users related to that chat through the corresponding event or group it depending of the chatable_type in each case, to learn more see Laravel Many To Many Polymorphic Relations
chats
we need also one table to save all conversations for specific chat
messages
and for users chat you can create a separate table witch can save the related information about that chat specifiquely
conversations
all sender_id and receiver_id will be foreign key witch reference some id on users table
Upvotes: 1
Reputation: 653
I believe that Chat
has a polymorphic association with Group
and Event
(and maybe User
as well). Some solutions to the problem are provided here:
https://www.slideshare.net/billkarwin/practical-object-oriented-models-in-sql/29-Polymorphic_Assocations_Solutions_Exclusive_arcs
Could something like this work?
ChatUser
Chat (exclusive arcs method)
ChatMessage
Assumptions made:
Besides, Laravel comes with a native polymorphic relation handling solution. It could fit here as well.
Upvotes: 5