对话,多对多关系 [英] Conversation, Many-to-Many relationship
问题描述
为了这个目的,我想创建一个对话
模型。据我所知,我将使用多对多关系。
拥有以下型号:对话
,用户
和消息
,我想象了以下表格:
对话: id | user1_id | user2_id
- 我不知道Laravel会理解用户ID是否编号
消息: id |消息| conversation_id | user_id
这样做是正确的吗?并且它将与 user1_id
和 user2_id
表格
我会建议:
- 多对多:用户< - >对话。一个
用户
可以有很多对话
s,一个对话
两个或多个用户
s - 一对多:用户< - >消息。
消息
属于一个用户
。一个用户
有很多消息
s - 一对多:消息< - >对话。
消息
属于一个对话
。一个对话
有许多消息
s。
这将给你以下SQL结构(只有列出的关系有趣的属性):
users:id
消息:id | user_id | conversation_id
对话:id
conversations_users:conversation_id | user_id
想想聊天室之间的对话,它几个用户之间基本上是一个共享的邮件集合。
在Laravel,可能的雄辩模型如下所示:
class User Enloquent
{
public function conversations()
{
return $ this-> belongsToMany('Conversation');
}
public function messages()
{
return $ this-> hasMany('Message'); //不相关,因为这些都是通过对话的消息
}
}
类消息扩展Eloquent
{
public function user()
{
return $ this-> belongsTo('User');
}
public function conversation()
{
return $ this-> belongsTo('Conversation');
}
}
class对话扩展Eloquent
{
public function messages()
{
return $ this-> ;的hasMany( '消息');
}
public function users()
{
return $ this-> belongsToMany('User');
}
}
//用户对话
$ conversations = User :: find(1) - >对话;
foreach($ conversations as $ conversation)
{
//所有会话成员
$ members = $ conversation-> users;
//所有会话消息
$ messages = $ conversation->消息;
}
I am developing an application where I need users to interact with each other using a chat-like system.
To this purpose, I want to create a Conversation
model. As far as I've been able to read, I am going to use a Many-to-Many relationship.
Having the following models: Conversation
, User
and Message
, I imagined the following tables:
conversations: id | user1_id | user2_id
- I am not sure if Laravel would understand the user IDs being numbered
messages: id | message | conversation_id | user_id
Would this be the right way to do it? And will it work with the user1_id
and user2_id
tables?
I would suggest:
- Many-to-many: User <-> Conversation. A
User
can have manyConversation
s, aConversation
consists of two or moreUser
s - One-to-many: User <-> Message. A
Message
belongs to oneUser
. AUser
has manyMessage
s - One-to-many: Message <-> Conversation. A
Message
belongs to oneConversation
. AConversation
has manyMessage
s.
This will give you following SQL structure (only attributes interesting for the relationships listed):
users: id
messages: id | user_id | conversation_id
conversations: id
conversations_users: conversation_id | user_id
Think about conversations like chat rooms, its basically one shared collection of messages between a couple of users.
In Laravel, possible Eloquent models would look like this:
class User extends Eloquent
{
public function conversations()
{
return $this->belongsToMany('Conversation');
}
public function messages()
{
return $this->hasMany('Message'); // not as relevant, because these are all messages across conversations
}
}
class Message extends Eloquent
{
public function user()
{
return $this->belongsTo('User');
}
public function conversation()
{
return $this->belongsTo('Conversation');
}
}
class Conversation extends Eloquent
{
public function messages()
{
return $this->hasMany('Message');
}
public function users()
{
return $this->belongsToMany('User');
}
}
// Users conversations
$conversations = User::find(1)->conversations;
foreach($conversations as $conversation)
{
// All members of conversation
$members = $conversation->users;
// All messages of conversation
$messages = $conversation->messages;
}
这篇关于对话,多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!