如何通过对laravel中的当前表列和关系表列进行查询来使用顺序? [英] How to use order by query both with current table column and relationship table column in laravel?
本文介绍了如何通过对laravel中的当前表列和关系表列进行查询来使用顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试通过查询按顺序获取记录,但这种情况是我必须在laravel的当前表列和关系表列上都按顺序使用order.我尝试过了
i am trying to fetch record with order by query but situation is this that i have to use order by both on current table column and relationship table column in laravel . I tried this
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with(['consignment_run_sheet' => function ($query) {
$query->orderBy('run_sheet_id');
}])->orderBy('delivery_date', 'DESC')->get();
$deliveryRuns = DeliveryRun::all();
我该如何实现?
推荐答案
它将以这种方式仅订购关系项目.解决方案是使用子查询或联接.如果假设consignment_run_sheet关系的模态为ConsignmentRunSheet且该关系为belongsTo :,则使用Subquery就像这样:
it will order just the relation items in this way. solution is use subquery or joins. useing Subquery is like this if assume the modal for consignment_run_sheet relation is ConsignmentRunSheet and the relation is belongsTo:
$consignments = Consignment::where('delivery_run_id', $id)->whereIn('status', [
'In Warehouse',
'With On Forwarder',
'In Transit (Warehouse->Delivery)',
'Awaiting Pickup'
])->with('consignment_run_sheet')
->orderBy(ConsignmentRunSheet::select('delivery_date')
->whereColumn('consignments.id', 'consignment_run_sheet.consignment_id'), 'DESC')
->get()
来源: https://reinink.ca/articles/ordering-laravel中按关系列查询数据库
这篇关于如何通过对laravel中的当前表列和关系表列进行查询来使用顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文