我试图为我的应用程序建立一个简单的对话系统,但是我在计划这个结构时遇到了一些麻烦。当用户进入“对话”页面时,它应该列出用户是发送方或接收方的所有对话,并根据最新消息进行排序。然后,当用户打开特定会话时,它应该列出来自该会话的所有消息。我的计划是这样的:
在这里,我有麻烦-我想建立与对话的关系,外键将是'sender_id‘或'receiver_id’,并返回对话顺序的最新消息。我怎样才能做到这一点?你能给我一些提示吗?当我返回用户时,如何解决用户、会话和消息之间的关系以返回所有信息?
我真的很感谢你的帮助。
发布于 2019-03-08 17:21:42
首先,您应该使用如下消息定义会话关系:
会话模型
public function messages()
{
return $this->hasMany('App\Message');
}您还应该定义逆关系:
消息模型
public function conversation()
{
return $this->belongsTo('App\Conversation');
} 您可以用下面的代码显示用户的所有对话:
public function getConversations($userId)
{
$conversations = Conversation::where('sender_id',$userId)->orWhere('receiver_id',$userId);
return view('yourview', compact('conversations'));
}在您的观点中,您可以循环并找到每一次谈话中的每一条消息:
@foreach($conversations as $conversation)
@foreach($conversation->messages as $message)
{{$message->message}}
@endforeach
@endforeach在您的conversations表中,您可以拥有一个user_id FK,您的关系应该如下所示:
用户模型
public function conversations()
{
return $this->hasMany('App\Conversation', 'user_id');
}注意:,您也可以使用receiver_id或sender_id作为外键。
有了这种关系,您就可以从用户那里获得所有对话,如下所示:
$user = User::find($id);
$user->conversations; // This will return all conversations from that user您还可以使用有很多人通过关系从用户那里获取所有消息:
public function messages()
{
return $this->hasManyThrough('App\Message', 'App\Conversation');
}另一种方法
另一种方法是在users和conversations之间创建一个枢轴表。
conversation_user表
id
user_id
conversation_id这样,用户就可以进行许多会话(多到多的关系)。
您可以将messages表更改为:
id
conversation_id
receiver_id
sender_id
content
...您的消息模型
public function conversation()
{
return $this->belongsTo('App\Conversation', 'conversation_id');
}用户模型
public function conversations()
{
return $this->belongsToMany('App\Conversation');
}Conversation模型
public function users()
{
return $this->belongsToMany('App\User');
}
public function messages()
{
return $this->hasMany('App\Message');
}我认为这是最好的方法
如果你想得到谈话和信息:
$user = User::find($id);
foreach($user->conversations as $conversation)
{
foreach($conversation->messages as $message)
{
echo $message->content;
}
}另一种简单的方法是使用消息模型:
$userMessages = Message::where('receiver_id', $userId)->orWhere('sender_id',$userId)->get();但是这样你就只能得到消息了,你可以找到这样的对话:
foreach($userMessages->conversation as $conversation)
{
echo $conversation;
}发布于 2019-03-08 19:13:09
我的方法是:我的谈话有一个主题。每条消息都有一个conversation_id和一个发送消息的user_id。最后,在参与者表中,我将添加作为线程一部分但不一定在其中发送消息的用户。
所以你必须有一个只有一个主题的对话表,你必须有参与者表,这样你必须添加一个对话的那一部分的用户。最后,必须保存用户发送到会话的每条消息的消息表。
对于线程表:
Schema::create('conversations', function (Blueprint $table) {
$table->increments('id');
$table->string('subject');
$table->timestamps();
});对于messages表:
Schema::create('messages', function (Blueprint $table) {
$table->increments('id');
$table->integer('conversation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->text('body');
$table->timestamps();
});对于参与消息的任何用户:
Schema::create('participants', function (Blueprint $table) {
$table->increments('id');
$table->integer('conversation_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->timestamp('last_read')->nullable();
$table->timestamps();
});用户模型:
public function conversations()
{
return $this->belongsToMany('App\Conversation', 'user_id');
}会话模式:
public function messages()
{
return $this->belongsToMany('App\User', 'conversation_id');
}https://stackoverflow.com/questions/55067298
复制相似问题