duan89197 2017-02-07 11:44
浏览 35

Laravel按相关表排序

I have a little complicated relationship and I also want to order my results using column in the other table, let's see:

$book = Book::where('id', '=', $id)->with([
        'versions.titles' => function ($query) {

            //I want to paginate table "titles" 
            $query->groupBy('titles.title')->paginate(15);
        },
        'versions.titles.numbers' => function ($query) {

            // but I want to order it by "numbers.order_key"
            $query->orderBy('numbers.order_key', 'asc')->get();
        }])
        ->first();

Of course, this code doesn't work properly - titles column isn't sorted by numbers.order_key. Does anybody have any idea how to get it? I don't really want to use joins because of a complication of my relationships.

Update:

My models

Book:
-id

Title:
-id

Version:
-id
-book_id

titles_version:
-title_id
-version_id

Number:
-id
-book_id
-title_id

I want to obtain one nested record to paginate titles, e.g. $book->versions->titles->paginate(15), where titles have to be ordered by numbers.order_key.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用