如何在聊天应用 Laravel 中区分发送者和接收者?
How do I differentiate between sender and receiver in an chat app Laravel?
我正在 Laravel 5.7 中制作私人(一对一)聊天应用程序。
我认为问题出在架构上。如果 user1 已登录并且他创建了与 user2 的新对话。这将使用 conversation_id=1 创建。
下次当 user2 登录时,假设他将在发件人列中找到他的 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
请给我一个更好的解决方案。
我可能忽略了模式的一个或多个复杂性,但我想出了以下问题。
用户 -
保留现有用户 table.
对话 -
这将代表单个对话、发起用户和对话开始的日期。
- id
- user_id # user who initiated conversation
- created_at
conversation_user -
这将代表参与对话的每个用户。虽然您的要求声明每个对话都在两个用户之间进行,但这种结构也允许您在未来进行群组对话——如果需要的话。
您可以选择添加 read_at
时间戳来存储 date/time 用户最后阅读对话的时间。然后,这可用于推断用户在对话中是否有未读消息。
- id
- conversation_id
- user_id
- read_at # when user last read conversation
- created_at
消息 -
这将代表对话中的一条消息。只需要记录发送用户,因为可以阅读消息的用户由 conversation_user
table.
决定
- id
- conversation_id
- user_id # user who sent message
- message
- created_at
- updated_at
我正在 Laravel 5.7 中制作私人(一对一)聊天应用程序。 我认为问题出在架构上。如果 user1 已登录并且他创建了与 user2 的新对话。这将使用 conversation_id=1 创建。 下次当 user2 登录时,假设他将在发件人列中找到他的 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
请给我一个更好的解决方案。
我可能忽略了模式的一个或多个复杂性,但我想出了以下问题。
用户 - 保留现有用户 table.
对话 - 这将代表单个对话、发起用户和对话开始的日期。
- id
- user_id # user who initiated conversation
- created_at
conversation_user - 这将代表参与对话的每个用户。虽然您的要求声明每个对话都在两个用户之间进行,但这种结构也允许您在未来进行群组对话——如果需要的话。
您可以选择添加 read_at
时间戳来存储 date/time 用户最后阅读对话的时间。然后,这可用于推断用户在对话中是否有未读消息。
- id
- conversation_id
- user_id
- read_at # when user last read conversation
- created_at
消息 -
这将代表对话中的一条消息。只需要记录发送用户,因为可以阅读消息的用户由 conversation_user
table.
- id
- conversation_id
- user_id # user who sent message
- message
- created_at
- updated_at