LaravelVersion7.0,MySQL版本5.8
我将为livechat系统设计数据库。下面是相关的模型和表格。
User模型- users表(id, name, email, address..)
Guest模型- guests表(id, name, ipAddress)
Team模型- teams表(id, name, image)
team_has_users (id, team_id, user_id)
用户应该能够发送消息到User,Team,Guest。
以下是我能想到的一些可能的案例。
messsages表(id, content, from_id, to_id, from_model, to_model) //我认为这不足以为每个用户保存read_status。
messages表(id, content)
message_from_to表(id, message_id, from_id, to_id, read_status)
from_id将是users表或guests表的id。
但是to_id和read的状态对我来说有点棘手。
有谁可以帮我?
谢谢
发布于 2020-08-01 00:01:07
模特User,Guest和Team我看起来也很好。
但是,如果您想跟踪用户看到Message的原因,就需要跟踪它。
为此,message_from_to需要一个team_id:
$table->integer('team_id')->nullable();如果是直接消息,或者包含Team id,则Wich将为null。
PS :您仍然需要from_model & to_model for message_from_to,或者您知道是为User还是Guest。
下面是关于数据库结构的更好版本:
users
id
name
email
...
guests
id
name
ipAdress
teams
id
name
(https://laravel.com/docs/7.x/eloquent-relationships#many-to-many)
team_user
id_team
id_user
(https://laravel.com/docs/7.x/eloquent-relationships#one-to-many)
messages
id
content
from_id
from_type
team_id [nullable]
(https://laravel.com/docs/7.x/eloquent-relationships#one-to-many-inverse)
message_to
id
id_message
to_id
to_type
read_statusUser & Guest应该实现同一个类。
有了雄辩,你就可以做到:
$user->messages(); // return all message
$message->from(); // return User or Guest instance
$message->to(); // return User & Guest array
$message->isRead(Guest|User); // retourn boolean
$message->hasTeam(); // retourn booleanhttps://stackoverflow.com/questions/63200350
复制相似问题