Many-to-many relationships
Facebook provides users with various ways to interact. Currently, we only have the opportunity to request and insert posts. As is the case with Facebook, we want to have chats with our friends and colleagues. We will introduce two new entities to cover this.
The first entity is called Chat
, while the second entity is called Message
.
Before we start the implementation, we need to lay out a detailed plan of what those entities will enable us to do.
A user can have multiple chats, and a chat can belong to multiple users. This relationship allows us to have group chats with multiple users, as well as private chats between only two users. A message belongs to one user, but every message also belongs to one chat.
Model and migrations
When transferring this into real code, we must generate the Chat
model. The problem here is that we have a many-to-many relationship between users and chats. In MySQL, this kind of relationship requires a table to store...