Laravel 中的消息关系功能
Message relationship feature in Laravel
我正在构建一个消息系统,其中一个 Team
可以向所有 users
发送一条消息,而一个用户可以向一个特定的 Team
发送一条消息
我无法确定 Team
、Message
、User
模型之间的关系
我的想法是使用相同的 table 来发送消息,其中 sender_id 和 recipient_id 根据消息的创建者和收件人的不同而变化,这样 id 就可以匹配用户个人资料或团队 ID。 type
被定义为 broadcast
(如果它是团队向所有用户发送的消息)和 contact
(如果用户在我列出消息时以这种方式向团队发送消息)我可以按类型等过滤
留言table
下面是我为消息 table 考虑的 table 列:
Schema::create('messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('sender_id');
$table->integer('recipient_id');
$table->string('type');
$table->timestamps();
});
因为一个团队可以有很多消息和很多消息,并且可以属于许多团队,所以我打算创建一个名为 team_messages
的新枢轴 table,例如
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');
}
当涉及到用户与消息的关系时,如果用户可以发送消息并且能够列出团队发送给他们的消息,那么最佳选择是什么?
如果您对两种类型的消息使用相同的 table,您可以在 eloquent
中使用多态性
Schema::create('messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('message');
$table->morphs('sender');
$table->morphs('recipient');
$table->timestamps();
});
这将创建 sender_id、sender_type、reciever_id、reciever_type,您不需要枢轴 table
https://laravel.com/docs/8.x/migrations#column-method-morphs
对于关系你可以这样做
在Message.php
public function sender()
{
return $this->morphTo();
}
public function recipient()
{
return $this->morphTo();
}
在team.php
public function sendMessages()
{
return $this->morphMany(Message::class, 'sender');
}
public function recievedMessages()
{
return $this->morphMany(Message::class, 'recipient');
}
在user.php
public function sendMessages()
{
return $this->morphMany(Message::class, 'sender');
}
public function recievedMessages()
{
return $this->morphMany(Message::class, 'recipient');
}
查看更多信息https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-polymorphic-relations
MessageController.php
function create(){
//if team was sending
$team->sendMessage()->create([
'message' => "",
'sender_id => $user->id,
'sender_type' => base_class($user)
])
}
我正在构建一个消息系统,其中一个 Team
可以向所有 users
发送一条消息,而一个用户可以向一个特定的 Team
我无法确定 Team
、Message
、User
模型之间的关系
我的想法是使用相同的 table 来发送消息,其中 sender_id 和 recipient_id 根据消息的创建者和收件人的不同而变化,这样 id 就可以匹配用户个人资料或团队 ID。 type
被定义为 broadcast
(如果它是团队向所有用户发送的消息)和 contact
(如果用户在我列出消息时以这种方式向团队发送消息)我可以按类型等过滤
留言table
下面是我为消息 table 考虑的 table 列:
Schema::create('messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('sender_id');
$table->integer('recipient_id');
$table->string('type');
$table->timestamps();
});
因为一个团队可以有很多消息和很多消息,并且可以属于许多团队,所以我打算创建一个名为 team_messages
的新枢轴 table,例如
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');
}
当涉及到用户与消息的关系时,如果用户可以发送消息并且能够列出团队发送给他们的消息,那么最佳选择是什么?
如果您对两种类型的消息使用相同的 table,您可以在 eloquent
中使用多态性Schema::create('messages', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('message');
$table->morphs('sender');
$table->morphs('recipient');
$table->timestamps();
});
这将创建 sender_id、sender_type、reciever_id、reciever_type,您不需要枢轴 table
https://laravel.com/docs/8.x/migrations#column-method-morphs
对于关系你可以这样做
在Message.php
public function sender()
{
return $this->morphTo();
}
public function recipient()
{
return $this->morphTo();
}
在team.php
public function sendMessages()
{
return $this->morphMany(Message::class, 'sender');
}
public function recievedMessages()
{
return $this->morphMany(Message::class, 'recipient');
}
在user.php
public function sendMessages()
{
return $this->morphMany(Message::class, 'sender');
}
public function recievedMessages()
{
return $this->morphMany(Message::class, 'recipient');
}
查看更多信息https://laravel.com/docs/8.x/eloquent-relationships#one-to-many-polymorphic-relations
MessageController.php
function create(){
//if team was sending
$team->sendMessage()->create([
'message' => "",
'sender_id => $user->id,
'sender_type' => base_class($user)
])
}