[英]Laravel eloquent leftJoin ignores orderByDesc when used
好吧,我已经尝试了几天试图找出我的代码有什么问题。 我分别有一个chat_group
和chat_message
表,其中每个group
都有last_message
belongs to
发送到group
的每条message
。 当我想根据发送到组create_at
字段的最后一条消息获取所有组时, laravel join
忽略orderBy
。 这是我的一段代码和它的数据库结构:
chat_groups:
id,
name,
last_message_id,
created_at
chat_messages:
id:
text:
created_at
$groups = ChatGroup::with($relations)
->leftJoin('chat_messages', 'chat_groups.last_message_id', '=', 'chat_messages.id')
->withCount('members')
->orderBy('chat_messages.created_at', 'DESC')
->orderBy('chat_groups.created_at', 'DESC')
->get();
dd($groups->toArray());
我为我的问题找到了解决方案。 我在组 updated_at 字段上使用了 order by 子句,无需使用 leftJoin。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.