Laravel聊天消息关系

发布于 2025-02-09 06:22:05 字数 1807 浏览 2 评论 0原文

我想将聊天功能集成到我的应用程序中。因此,我创建了一个带有属性消息,发送者和接收器的消息模型。我看不到为聊天创建表的重点,因为除了消息模型给出的内容外,没有属性。但是,拥有一个模型和聊天资源将很方便。因此,对我而言,聊天基本上是两个用户之间的所有消息。
如何在没有自己的数据库表的情况下创建一个聊天模型,该数据库表将单个用户的消息分组,以及如何实现与用户模型的关系以返回用户的一部分?

class Message extends Model
{
    use HasFactory;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'message',
        'sender_id',
        'receiver_id'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [];

    /**
     * Get the user who sent this message
     */
    public function sender() {
        $this->hasOne(User::class, 'sender_id');
    }

    /**
     * Get the user who received this message
     */
    public function receiver() {
        $this->hasOne(User::class, 'receiver_id');
    }

    /**
     * Get the chat
     */
    public function chat() {
        $this->belongsTo(Chat::class);
    }
}

和迁移

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->text('message');
            $table->foreignId('sender_id')->references('id')->on('users');
            $table->foreignId('receiver_id')->references('id')->on('users');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('messages');
    }
};

I want to integrate a chat functionality to my application. Therefore, I created a Message model with the attributes message, sender and receiver. I don't see the point of creating a table for chats, as there would be no attributes besides the things given by the message model. However, having a model and a resource for chats would be handy. So, for me a chat is basically all messages between two users.
How can I create a chat model without its own database table that groups messages of individual users and how could the relationship to the users model be implemented to return all chats a user is part of?

class Message extends Model
{
    use HasFactory;
    use SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'message',
        'sender_id',
        'receiver_id'
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [];

    /**
     * Get the user who sent this message
     */
    public function sender() {
        $this->hasOne(User::class, 'sender_id');
    }

    /**
     * Get the user who received this message
     */
    public function receiver() {
        $this->hasOne(User::class, 'receiver_id');
    }

    /**
     * Get the chat
     */
    public function chat() {
        $this->belongsTo(Chat::class);
    }
}

and the Migration

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('messages', function (Blueprint $table) {
            $table->id();
            $table->text('message');
            $table->foreignId('sender_id')->references('id')->on('users');
            $table->foreignId('receiver_id')->references('id')->on('users');
            $table->timestamps();
            $table->softDeletes();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('messages');
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稳稳的幸福 2025-02-16 06:22:05

好吧,您实际上可以创建一个名为对话的表来保留两个实际聊天的用户。

发件人和接收者是谁,它只是持有两个用户的价值,首先聊天的人将是sender_id

这样的结构

conversation:
 sender_id,
 receiver_id

message: 
 content,
 conversation_id,
 sender_id

使发件人和接收器将足够灵活,以使您的消息模型可以加入关系并获得期望值。

Well, you can actually create a table named Conversation to hold the two users who actually chatting.

It doesn't matter much who is the sender and receiver, it's just holding the two users' value and whoever chatted first will be the sender_id.

A structure like this

conversation:
 sender_id,
 receiver_id

message: 
 content,
 conversation_id,
 sender_id

which holds the sender and receiver will be flexible enough for your message model to join the relationship and get the expected value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文