【问题标题】:Laravel sort conversations by last messageLaravel 按最后一条消息对对话进行排序
【发布时间】:2019-05-10 12:29:29
【问题描述】:

我有一个 Conversation 模型,其中包含许多 ConversationMessage 模型。

现在我想根据对话的最后一条消息对对话进行排序。基本上就像WhatsApp。如何构建查询?

作为我的代码的旁注:对话包含一个用户 (user_id) 和一家关联公司 (company_id)。公司与用户有belongsToMany 关系。

Conversation::whereHas('company', function ($q) use ($userId) {
    $q->whereHas('users', function ($q1) use ($userId) {
        $q1->where('users.id', $userId);
    });
})->orWhereHas('user', function($q) use ($userId) {
    $q->where('user.id', $userId);
})->with(array('conversationMessage' => function($q) {
    $q->orderBy('created_at', 'DESC');
})->get();

ConversationMessage 关系模型进行排序,而不是对话。如何根据最后一条消息对Conversations 进行排序?

【问题讨论】:

  • 在任何事情之前,我不确定,但我认为您可以在一行中写下您的第一个 whereHasConversation::whereHas('company.users', function ($q) use ($userId) { $q->where('users.id', $userId); })
  • 没错,谢谢!

标签: php sql laravel eloquent


【解决方案1】:

您可以将last_message_at 之类的timestamp 字段添加到您的Conversations 模型中,当有新消息添加到该Conversation 时更新该字段,然后您可以根据last_message_at 对结果进行排序。

或者您可以像下面这样在查询中过滤结果(测试它是否有效):

    Conversation::whereHas('company', function ($q) use ($userId) {
        $q->whereHas('users', function ($q1) use ($userId) {
            $q1->where('users.id', $userId);
        });
    })->orWhereHas('user', function($q) use ($userId) {
        $q->where('user.id', $userId);
    })->with(array('conversationMessage' => function($q) {
        $q->orderBy('created_at', 'DESC');
    })
    ->selectRaw("conversations.*, (SELECT MAX(created_at) from conversation_messages WHERE conversation_messages.conversation_id=conversations.id) as latest_message_on")
    ->orderBy("latest_message_on", "DESC")
    ->get();

【讨论】:

  • 这也是我在考虑的一个选项,但这将是每条新消息的更新查询。就我个人而言,如果可能的话,我会发现对关系进行排序会更好
  • 这是对答案的更新,测试它是否有效。
猜你喜欢
  • 2021-10-17
  • 1970-01-01
  • 1970-01-01
  • 2021-02-27
  • 2016-10-19
  • 2016-03-24
  • 1970-01-01
  • 1970-01-01
  • 2019-12-26
相关资源
最近更新 更多