首先,我尝试查看this,但我无法使解决方案正常工作,因为它涉及到数据透视表,而我没有。我的大脑无法从该解决方案中抽象出解决方案。
我有这个问题:
$orderLines = PurchasingOrderLine::where('season_id', '=', $seasonRecID)
->with('factory')
->with('purchasingFactoryDates.milestone')
->with('divergedColors.purchasingColorDates.milestone')
->get();
所以这是一段很大的关系。
我想按purchasingFactoryDates
的“数字”属性对milestone
进行排序。
以下是此查询的JSON响应示例(示例)..
{
id: 1,
season_id: 258,
factory_id: 38,
archived: 0,
created_at: "2016-10-03 00:00:00",
updated_at: "2016-10-04 00:00:00",
factory: {
ID: 38,
name: "testfac",
address: "testadrr",
postCode: "",
city: "Foo",
countryID: 27,
podID: 2,
warehouse_id: 3,
paymentTermID: 11,
factoryCode: "Bazz",
active: 1
},
purchasing_factory_dates: [
{
id: 1,
purchasing_order_line_id: 1,
milestone_id: 1,
milestone_date: "1993-10-22 19:15:51",
milestone_status_id: 2,
created_at: "2016-10-04 07:38:30",
updated_at: "2016-10-18 19:15:51",
milestone: {
id: 1,
name: "test1",
description: null,
number: 1,
created_at: "-0001-11-30 00:00:00",
updated_at: "-0001-11-30 00:00:00"
}
},
.........
关于如何做到这一点的任何想法?我不希望整个关系按Milestone.number
排序,只需按purchasingFactoryDates
排序。
答案 0 :(得分:1)
最简单的方法是加入这样的查询:
$orderLines = PurchasingOrderLine::where('season_id', '=', $seasonRecID)
->select('purchasing_order_lines.*')
->join('purchasing_factory_dates', 'purchasing_factory_dates.purchasing_order_line_id', '=', 'purchasing_order_lines.id')
->join('milestone', 'purchasing_factory_dates.milestone_id', '=', 'milestone.id')
->with('factory')
->with('purchasingFactoryDates.milestone')
->with('divergedColors.purchasingColorDates.milestone')
->groupBy('purchasing_order_lines.id')
->orderBy('milestone.number')
->get();
答案 1 :(得分:0)
$orderLines = PurchasingOrderLine::with('purchasingFactoryDates'=>function($query)
{
$query->with(['milestone'=>function($query)
{
$query->orderBy('number');
}]);
}]);
答案 2 :(得分:0)
我认为这可能是你想要的,与你正在看的其他答案相近。
$orderLines = PurchasingOrderLine::where('season_id', '=', $seasonRecID)
->with('factory')
->with(['purchasingFactoryDates' => function($q) {
$q->select('purchasingFactoryDates.*')->join('milestone', 'purchasingFactoryDates.milestone_id', '=', 'milestone.id')->orderBy('milestone.number');
}, 'purchasingFactoryDates.milestone'])
->with('divergedColors.purchasingColorDates.milestone')
->get();