【问题标题】:Message relationship feature in LaravelLaravel 中的消息关系功能
【发布时间】:2021-05-28 16:42:26
【问题描述】:

我正在构建一个消息系统,其中Team 可以向所有users 发送消息,并且用户可以向特定Team 发送消息

我无法确定TeamMessageUser 模型之间的关系

我的想法是使用同一张表发送消息,其中 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


    【解决方案1】:

    如果你对两种类型的消息都使用同一个表,你可以在 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 并且您不需要数据透视表

    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_id 和用户 id 可能不同,该解决方案仍能提供创建和检索消息记录的最佳方式吗?
    • 例如,如果您从团队向用户发送消息,它会将 sender_id 记录为 team_id 并将 sender_type 记录为团队模型,并且对于收件人 user_id 和用户模型将被存储。所以如果你想检查一个team sendMessages $team->sendMessages 你会得到一个团队发送的所有消息
    • 谢谢,那么如果你想创建一个消息作为团队发送/存储 -> 用户你会做 $team->sendMessages()->create() 还是 $team ->message()->create() 我误会了吗?
    • 我如何创建从团队发送给用户的消息记录?
    • 在同一张表中,您可以创建多列消息。
    猜你喜欢
    • 1970-01-01
    • 2014-10-15
    • 1970-01-01
    • 2023-01-26
    • 2015-01-02
    • 1970-01-01
    • 2021-02-05
    • 2020-01-03
    • 1970-01-01
    相关资源
    最近更新 更多