消息列表页,每个会话最新一条消息
public function index()
{
$userId = user()->id;
$msgIds = Message::selectRaw('max(id) as id')
->where('from_user_id', $userId)
->orWhere('to_user_id', $userId)
->groupBy('dialog_id')
->pluck('id')
->toArray();
$messages = Message::with('toUser', 'fromUser')
->whereIn('id', $msgIds)
->get();
return view('inbox.index', ['messages' => $messages->keyBy('to_user_id')]);
}