繁体   English   中英

如何通过 Laravel 中的嵌套关系订购

[英]How to orderBy with a nested relationship in Laravel

我有以下代码:

$invoices = \App\Finance_Invoices::query()->with("file.property")->where('period', "02")->where('year', "2022")->where('paid_to_landlord', 0)->whereColumn('total_paid', '>=', 'amount')->whereHas('file.property.landlord', function ($query) use ($id) {
                $query->where('id', $id);
            })->orderBy("file.property.full_address")->get();

这个想法是得到未结清的发票,但按地址排序。 该地址仅可通过关系“文件-> 属性”获得。

使用此代码,我收到此错误:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'file.property.full_address' in 'order clause' (SQL: select * from `finance_invoices` where `period` = 02 and `year` = 2022 and `paid_to_landlord` = 0 and `total_paid` >= `amount` and exists (select * from `files` where `finance_invoices`.`file_id` = `files`.`id` and exists (select * from `properties` where `files`.`property_id` = `properties`.`id` and exists (select * from `landlords` where `properties`.`landlord_id` = `landlords`.`id` and `id` = 1097))) order by `file`.`property`.`full_address` asc)

谁能解释一下该怎么做?

使用错误消息中的语法,您无法从文件表中检索或排序列。 您只能从 finance_invoices 表中检索或排序列。 我建议使用 JOIN 而不是嵌套的子查询。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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