【发布时间】:2021-05-28 16:42:26
【问题描述】:
我正在构建一个消息系统,其中Team 可以向所有users 发送消息,并且用户可以向特定Team 发送消息
我无法确定Team、Message、User 模型之间的关系
我的想法是使用同一张表发送消息,其中 sender_id 和 recipient_id 会根据创建消息的人和收件人的身份而变化,因此 id 可以匹配用户个人资料或团队 id。 type 被定义为broadcast,如果它是来自团队的消息给所有用户,如果用户以这种方式向团队发送消息,则定义为contact,当我列出消息时,我可以按类型过滤等.
消息表
下面是我为 Messages 表想到的表列:
Schema::create('messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('sender_id');
$table->integer('recipient_id');
$table->string('type');
$table->timestamps();
});
因为一个团队可以有很多消息和很多消息,并且可以属于多个团队,所以我打算创建一个名为 team_messages 的新数据透视表@ 例如
public function up()
{
Schema::create('team_messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('team_id');
$table->integer('message_id');
});
}
团队模型内部的关系是:
public function messages()
{
return $this->belongsToMany('App\Message');
}
当涉及到用户与消息的关系时,如果用户可以发送消息并且能够列出团队发送的消息,那么最好的选择是什么?
【问题讨论】:
标签: laravel eloquent pivot-table relationship