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

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您实际上可以创建一个名为
对话
的表来保留两个实际聊天的用户。发件人和接收者是谁,它只是持有两个用户的价值,首先聊天的人将是
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
which holds the sender and receiver will be flexible enough for your message model to join the relationship and get the expected value.