gpt4 book ai didi

php - Laravel 通过嵌套的急切加载关系排序

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:05 25 4
gpt4 key购买 nike

首先,我尝试查看 this ,但我无法使解决方案起作用,因为它涉及数据透视表,而我的没有。我的大脑无法从该解决方案中抽象出解决方案。

我有这个问题:

$orderLines = PurchasingOrderLine::where('season_id', '=', $seasonRecID)
->with('factory')
->with('purchasingFactoryDates.milestone')
->with('divergedColors.purchasingColorDates.milestone')
->get();

所以这是一个很大的关系。

我想按 milestone 的“number”属性对 purchasingFactoryDates 进行排序。

这是此查询的 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 排序。

最佳答案

最简单的方法是像这样加入查询:

$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();

关于php - Laravel 通过嵌套的急切加载关系排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40132447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com