1

In my web application I want to add support for messaging eachother. There can be two or more people in one chat, and I have set up my tables as follows:

+------------------------+
| Messages               |
+------------------------+
| id                     |
| conversation_id        |
| user_id                |
| body                   |
+------------------------+

+------------------------+
| Conversations          |
+------------------------+
| id                     |
| listing_id             | --> references a listing model
+------------------------+

+------------------------+
| conversation_user      |
+------------------------+
| id                     |
| conversation_id        |
| user_id                |
+------------------------+

All conversations are related to a Listing so that is why I have this table here.

When I want to initiate a new conversation, I first have to check that it is unique for the user and listing.

Then, I must create a new conversation and a new message. And then instert the message as a related model to the conversation model.

How would I do this? I think I have tried everything from relating each way, and associating and save and create etc etc. But I can't figure it how :(

rebellion
  • 6,628
  • 11
  • 48
  • 79

0 Answers0