【问题标题】:How do I differentiate between sender and receiver in an chat app Laravel?如何区分聊天应用 Laravel 中的发送者和接收者?
【发布时间】:2018-09-27 21:37:39
【问题描述】:

我正在 Laravel 5.7 中制作一个私人(一对一)聊天应用程序。 我认为问题出在架构上。如果 user1 已登录并与 user2 建立新对话。这将使用 conversation_id=1 创建。 下次当 user2 登录时,假设他将在 sender 列中找到他的 ID 而他不会在 sender_column 中找到他的 ID,将创建不应创建的新对话,因为要创建新对话,我们必须同时检查发送方和接收方列。

这是我的架构

    =======User Table========
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

    =============Chat Table=============
    Schema::create('chats', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('user1_id')->nullable();
        $table->foreign('user1_id')->references('id')->on('users');
        $table->unsignedInteger('user2_id')->nullable();
        $table->foreign('user2_id')->references('id')->on('users');
        $table->timestamps();
    });

    ===============Messages Table================
    Schema::create('messages', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('chat_id')->nullable();
        $table->foreign('chat_id')->references('id')->on('chats');
        $table->text('messages');
        $table->timestamps();

    });

也许你就这样理解了。

Users                            Chats                                
id    name          chat_id    user1_id    user2_id
1     Mark             1           1           2
2     David            2           2           1
3     Henzard          3           2           3
                       4           3           2

请给我一个更好的解决方案。

【问题讨论】:

    标签: database laravel-5 eloquent chat


    【解决方案1】:

    我可能忽略了架构的一个或多个复杂性,但我想出了以下内容。

    用户 - 保留现有的用户表。

    对话 - 这将代表单个对话、发起用户以及对话开始的日期。

    - id
    - user_id           # user who initiated conversation
    - created_at
    

    conversation_user - 这将代表参与对话的每个用户。虽然您的要求表明每个对话都在两个用户之间进行,但这种结构也允许您在未来进行群组对话 - 如果需要的话。

    或者,您可以添加read_at 时间戳来存储用户上次阅读对话的日期/时间。这可以用来推断用户在对话中是否有未读消息。

    - id
    - conversation_id
    - user_id
    - read_at           # when user last read conversation
    - created_at
    

    消息 - 这将代表对话中的一条消息。只需要记录发送用户,因为可以阅读消息的用户由conversation_user表决定。

    - id
    - conversation_id
    - user_id           # user who sent message
    - message
    - created_at
    - updated_at
    

    【讨论】:

    • 那么我如何让他参与的用户对话?我是否必须为此检查两个表?
    • 您能否澄清一下,您是在问如何获取单个用户参与的所有对话吗?或者如何获得特定的对话?
    • 假设 Mark 是否使用他的帐户登录。我想获得所有涉及 Mark 的消息,无论他是发送者还是接收者。我是否必须同时检查对话和对话用户表才能获取他参与的那些消息。
    • 不,我会将发送者和接收者都添加到conversation_user 表中。
    • 那么我应该把conversation_user表中的user_id做成一个数组来存储发送者和接收者吗?
    猜你喜欢
    • 2023-03-03
    • 2019-10-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2013-01-19
    • 2014-01-12
    • 1970-01-01
    • 2021-03-01
    相关资源
    最近更新 更多