Reputation: 1095
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 StackOverflow, but haven't quite found what I am after- in terms of database design.
Here is what I was thinking:
messages
:
users
.user_id
message_recipients
:
messages
.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?
Feel free to suggest a different design :)
Upvotes: 1
Views: 1971
Reputation: 26258
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.
Upvotes: 2
Reputation: 163758
You can create just one table with id
, sender_id
, recipient_id
, message
.
sender_id
and recipient_id
are foreign keys pointed to users
table.
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.
Upvotes: 1