我有messages
表,其结构如下:id
,to_id
,from_id
...
我需要编写Eloquent查询,该查询为我提供了针对特定用户的对话框。每个对话框都应该有最后一条消息(无论是传入还是发送),并且所有消息均由capped_at desc排序。
例如,通过同伴ID获得整个对话框:
$messages = Message::with(array_keys($fields->getRelations()))
->select($select)
->whereRaw('from_id = ? and to_id = ? ', [auth()->id(), $args['user_id']])
->orWhereRaw('from_id = ? and to_id = ? ', [$args['user_id'], auth()->id()])
->orderByDesc('created_at');
但是在此之前,我需要提供一系列对话框。
尝试
$dialogUsers = Message::where('to_id', auth()->id())
->orWhere('from_id', auth()->id())
->select(['from_id', 'to_id'])
->distinct();
但是我也需要按降序获得消息