繁体   English   中英

Laravel 雄辩的 leftJoin 使用时忽略 orderByDesc

[英]Laravel eloquent leftJoin ignores orderByDesc when used

好吧,我已经尝试了几天试图找出我的代码有什么问题。 我分别有一个chat_groupchat_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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM