cinek181992
cinek181992

Reputation: 155

Design for chats between users

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

Answers (2)

Yves Kipondo
Yves Kipondo

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

  • id
  • event_id
  • user_id

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

  • id
  • group_id
  • user_id

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

  • id
  • name
  • chatable_id
  • chatable_type

we need also one table to save all conversations for specific chat

messages

  • id
  • chat_id
  • message
  • created_at

and for users chat you can create a separate table witch can save the related information about that chat specifiquely

conversations

  • id
  • sender_id
  • receiver_id
  • message

all sender_id and receiver_id will be foreign key witch reference some id on users table

Upvotes: 1

Andrius Rimkus
Andrius Rimkus

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

  • id
  • user_id
  • chat_id

Chat (exclusive arcs method)

  • id
  • event_id (nullable)
  • group_id (nullable)
  • description/etc

ChatMessage

  • id
  • chat_id
  • user_id_from
  • user_id_to (nullable)
  • date
  • message

Assumptions made:

  • event/group can contain multiple chats
  • user can belong to a group but not to group chat

Besides, Laravel comes with a native polymorphic relation handling solution. It could fit here as well.

Upvotes: 5

Related Questions