内部消息传递系统laravel数据库设计

时间:2022-06-11 12:46:37

I am building an internal messaging system for a forum that I have built with Laravel 5.3. Basically, I want to have messaging functionality, similar to Linkedin- User can send an email to another User and vice versa. There is going to be a single thread for a given conversation, which will take place between two users. I have been looking through some questions on *, but haven't quite found what I am after- in terms of database design.

我正在为我使用Laravel 5.3构建的一个论坛构建一个内部消息传递系统。基本上,我希望有消息传递功能,类似于Linkedin——用户可以向另一个用户发送电子邮件,反之亦然。对于给定的会话,将会有一个线程,这将发生在两个用户之间。我一直在研究*上的一些问题,但是还没有找到我想要的东西——在数据库设计方面。

Here is what I was thinking:

以下是我的想法:

messages:

消息:

  • id PK
  • id PK
  • sender_id FK to users.user_id
  • sender_id颗,users.user_id
  • content
  • 内容

message_recipients:

message_recipients:

  • id PK
  • id PK
  • message_id FK to messages.id
  • message_id颗,messages.id
  • recipient_id FK to users.id
  • recipient_id颗,users.id

However, I am not 100% sure on the types of relationships there should be between the users, messages and message_recipients. Users would have many messages. Messages would belong to a User. But, how would I factor in the message_recipients table?

但是,我不能100%确定用户、消息和message_receiver之间的关系类型。用户会有很多消息。消息将属于用户。但是,如何将message_receiver表分解?

Feel free to suggest a different design :)

请随意提出不同的设计:)

2 个解决方案

#1


1  

You can create just one table with id, sender_id, recipient_id, message.

您可以使用id、sender_id、收信人id、消息创建一个表。

sender_id and recipient_id are foreign keys pointed to users table.

sender_id和attribuent_id是指向users表的外键。

It's many to many relationship and you can treat this table as pivot, so you can define belongsToMany relations. Also you can create model for this table if you will work with it a lot.

它有很多关系,你可以把这个表当作轴心,所以你可以定义归属感关系。您还可以为该表创建模型,如果您愿意使用它的话。

#2


1  

Try this kind of structure:

试试这种结构:

messages:

id          - int, autoincrement id
parent_id   - int. To maintain message threads
sender_id   - int. Senders Id
recipient_id- int. Recievers Id
content     - text, Message content
status      - enum('0', '1') for Read, Unread, Deleted etc

or break it into 2 or 3 table by maintaining the foreign key relationship.

或者通过维护外键关系将其分解为2或3个表。

#1


1  

You can create just one table with id, sender_id, recipient_id, message.

您可以使用id、sender_id、收信人id、消息创建一个表。

sender_id and recipient_id are foreign keys pointed to users table.

sender_id和attribuent_id是指向users表的外键。

It's many to many relationship and you can treat this table as pivot, so you can define belongsToMany relations. Also you can create model for this table if you will work with it a lot.

它有很多关系,你可以把这个表当作轴心,所以你可以定义归属感关系。您还可以为该表创建模型,如果您愿意使用它的话。

#2


1  

Try this kind of structure:

试试这种结构:

messages:

id          - int, autoincrement id
parent_id   - int. To maintain message threads
sender_id   - int. Senders Id
recipient_id- int. Recievers Id
content     - text, Message content
status      - enum('0', '1') for Read, Unread, Deleted etc

or break it into 2 or 3 table by maintaining the foreign key relationship.

或者通过维护外键关系将其分解为2或3个表。