如何正确设置内部 laravel 网站消息数据库

How to set up an internal laravel website Messaging Database correctly

我正在努力设置我的消息传递数据库。我允许用户通过我的网站互相发送消息。当用户进入他们的消息面板时,我希望数据库 return 这样的数据

1[
Message with sender_id = 1 and sent_to_id = 2 body = text here
Message with sender_id = 2 and sent_to_id = 1 body = text here
Message with sender_id = 1 and sent_to_id = 2 body = text here
]
2[
Message with sender_id = 1 and sent_to_id = 4 body = text here
Message with sender_id = 4 and sent_to_id = 1 body = text here
]
3[
Message with sender_id = 16 and sent_to_id = 1 body = text here
Message with sender_id = 1 and sent_to_id = 16 body = text here
]

我开始像这样创建我的数据库

$table->bigIncrements('id');
            $table->unsignedBigInteger('sender_id');
            $table->unsignedBigInteger('sent_to_id');
            $table->text('body');
            $table->timestamps(); 
            $table->foreign('sender_id')
                    ->references('id')->on('users')
                    ->onDelete('cascade');
            $table->foreign('sent_to_id')
                    ->references('id')->on('users')
                    ->onDelete('cascade');

然后在我的用户模型中我有

public function received()
    {
        return $this->hasMany(Message::class, 'sent_to_id');
    }
 public function sent()
    {
        return $this->hasMany(Message::class, 'sender_id');
    }

效果很好,但我无法像我的第一个示例那样将消息分组到用户之间的对话中。

  1. 我是否正确设置了我的数据库以实现我想要实现的目标?

  2. 如何在我的模型中将其设置为 return 消息按用户之间的对话分组?

您可以使用 sender_idrecipient_id 创建一个 conversations table 和一个单独的 messages table 来存储属于到那次谈话。

/** Conversations **/
class Conversation extends Model
{

    protected $fillable = ['sender_id', 'sent_to_id'];

    public function messages()
    {
        return $this->hasMany(Message::class);
    }
    ...

/** Messages **/
class Message extends Conversation
{
    protected $fillable = [
        'user_id',// User that created this message
        'message', // Body of the message
        'conversation_id'
    ];

    public function conversation()
    {
        return $this->belongsTo(Conversation::class);
    }
    ...

通过上述设置,您可以通过消息进行对话:

/** Currently authenticated user **/
$userId = auth()->id();
$conversations = App\Conversation::where('sender_id', $userId)
    ->orWhere('sent_to_id', $userId)
    ->with('messages')->get();

您可以发送消息 table 其中包含:

  • sender_id 整数
  • receiver_id 整数
  • 正文 varchar(255)
  • created_at 日期时间
  • seen_at 日期时间可为空
    /** Messages **/
    class Message extends Model
    {
        protected $fillable = [
            'sender_id',
            'receiver_id',
            'body',
            'seen_at',
        ];
    }

并使用 :

检索它
// don't  forget to update seen attribute while fetching
App\Message::where('receiver_id',$userId)->update(['seen_at' => date('Y-m-d H:i:s') ]);
//
$userId = auth::user()->id(); // authenticated_user
$messages = App\Message::where('sender_id', $userId)->orWhere('receiver_id',$userId)->get();

您可以轻松地在其上进行映射以设置样式(在视图中)

@foreach($messages as $message)
  @if ($message->sender_id == Auth::user()->id)
    <p class='message_sent'> {{$message->body}} </p>
  @else
    <p class='message_received'> {{$message->body}} </p>
  @endif
 @if ($message->seen_at)
  <p> {{$message->seen_at}} </p>
 @endif
@endforeach