我正在为Laravel 4创建简单的信使,其功能与Facebook信使非常相似。我有问题使用Eloquent查询列出当前登录用户的线程的最新消息。
考虑我有以下用于Messenger的数据库表(还有用户表):
+-----------+ +--------------+ +----------------+ +----------------+ +-----+
|threads | |thread_users | |messages | |messages_status | |users|
+-----------+ +--------------+ +----------------+ +----------------+ +-----+
|id | |id | |id | |id | |id |
|title | |thread_id | |thread_id | |user_id | |name |
|created_at | |user_id | |sender_id (user)| |message_id | +-----+
|deleted_at | |created_at | |content | |deleted_at |
+-----------+ |deleted_at | |created_at | +----------------+
+--------------+ +----------------+
数据库说明:
当用户写第一条消息时,他会创建线程。添加到线程的所有用户都列在thread_users表中。提交给线程的消息存储在消息表中,消息表链接到messages_status表,如果用户从线程中删除了特定消息,我将存储信息。
Laravel模型(我已删除SoftDeletingTrait以明确说明):
class threads extends \Eloquent {
protected $table = 'threads';
public function participants() {
return $this->hasMany('messengerThreadUsers', 'thread_id', 'id');
}
public function userParticipation() {
return $this->hasOne('threadUsers', 'thread_id', 'id')->where('user_id', '=', Auth::user()->id);
}
public function messages() {
return $this->hasMany('message', 'thread_id', 'id');
}
public function lastMessage() {
return $this->hasOne('message', 'thread_id', 'id')->latest();
}
}
class threadUsers extends \Eloquent {
protected $table = 'thread_users';
}
class message extends \Eloquent {
protected $table = 'messages';
public function statuses() {
return $this->hasMany('messageStatus', 'message_id', 'id');
}
public function thread() {
return $this->hasOne('threads', 'id', 'thread_id');
}
}
class messageStatus extends \Eloquent {
protected $table = 'messages_status';
public function message() {
return $this->hasOne('message', 'id', 'message_id');
}
public function thread() {
return $this->hasOne('threads', 'id', 'thread_id');
}
}
现在我想查询返回5条最新消息(按消息创建日期排序)和线程,对于当前登录的用户,但message_status.deleted_at为NULL。
到目前为止,我已经得到了这个:
$this->threads->select(\DB::raw('threads.*'))
->with('lastMessage')
->has('userParticipation')
->join('messages', 'messages.thread_id', '=', 'threads.id')
->orderBy('messages.created_at', 'DESC')
->paginate(5);
但它不断回复我的重复线程。在DB中我有2个线程:
线程#1中有2条消息
线程#2中有1条消息
输出如下:
0 =>
array (size=5)
'id' => int 10
'title' => string 'THREAD 1' (length=8)
'deleted_at' => null
'created_at' => string '2015-07-06 14:20:03' (length=19)
'last_message' =>
array (size=7)
'id' => int 11
'sender_id' => int 12
'thread_id' => int 10
'content' => string 'MSG 1 in thread 1' (length=129)
'created_at' => string '2015-07-07 19:36:52' (length=19)
1 =>
array (size=5)
'id' => int 10
'title' => string 'THREAD 1' (length=8)
'deleted_at' => null
'created_at' => string '2015-07-06 14:20:03' (length=19)
'last_message' =>
array (size=7)
'id' => int 11
'sender_id' => int 12
'thread_id' => int 10
'content' => string 'MSG 1 in thread 1' (length=129)
'created_at' => string '2015-07-07 19:36:52' (length=19)
2 =>
array (size=5)
'id' => int 10
'title' => string 'THREAD 2' (length=8)
'deleted_at' => null
'created_at' => string '2015-07-06 14:25:13' (length=19)
'last_message' =>
array (size=7)
'id' => int 11
'sender_id' => int 12
'thread_id' => int 10
'content' => string 'MSG 1 in thread 2' (length=129)
'created_at' => string '2015-07-07 19:56:22' (length=19)