[英]Laravel Eloquent Nested Eager Load Apply Order By
鉴于此数据:
{
"current_page": 1,
"data": [
{
"id": 1,
"delivery_id": 1,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Monday",
"location": {
"direction": "North"
}
}
}
},
{
"id": 2,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "09/28/2022",
"delivery" {
"day": "Friday",
"location": {
"direction": "South"
}
}
}
},
{
"id": 3,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Wednesday",
"location": {
"direction": "Northeast"
}
}
}
},
{
"id": 4,
"delivery_id": 3,
"deliveries_sub": {
"first_delivery_date": "09/02/2022",
"delivery" {
"day": "Tueday",
"location": {
"direction": "North"
}
}
}
}
],
"first_page_url": "http://localhost/mypage/list?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://localhost/mypage/list?page=1",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://localhost/mypage/list?page=1",
"label": "1",
"active": true
},
{
"url": null,
"label": "Next »",
"active": false
}
],
"next_page_url": null,
"path": "http://localhost/mypage/list",
"per_page": 10,
"prev_page_url": null,
"to": 4,
"total": 4
}
我想按first_delivery_date对其进行排序。 我已经使用以下方法获取了这个:
DeliveryDays::with('deliverySub.delivery.location')
->orderBy('deliverySub.first_delivery_date')
->paginate(10);
似乎orderBy子句不起作用。 我也尝试过这种方法:
DeliveryDays::with(['deliverySub' => function ($query) {
$query->orderBy('first_delivery_date');
}, 'deliverySub.delivery.location'])
->paginate(10);
但它也不起作用。 我想要的结果是排序后的first_delivery_date数据:
{
"current_page": 1,
"data": [
{
"id": 1,
"delivery_id": 1,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Monday",
"location": {
"direction": "North"
}
}
}
},
{
"id": 3,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "08/31/2022",
"delivery" {
"day": "Wednesday",
"location": {
"direction": "Northeast"
}
}
}
},
{
"id": 4,
"delivery_id": 3,
"deliveries_sub": {
"first_delivery_date": "09/02/2022",
"delivery" {
"day": "Tueday",
"location": {
"direction": "North"
}
}
}
},
{
"id": 2,
"delivery_id": 2,
"deliveries_sub": {
"first_delivery_date": "09/28/2022",
"delivery" {
"day": "Friday",
"location": {
"direction": "South"
}
}
}
}
],
"first_page_url": "http://localhost/mypage/list?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://localhost/mypage/list?page=1",
"links": [
{
"url": null,
"label": "« Previous",
"active": false
},
{
"url": "http://localhost/mypage/list?page=1",
"label": "1",
"active": true
},
{
"url": null,
"label": "Next »",
"active": false
}
],
"next_page_url": null,
"path": "http://localhost/mypage/list",
"per_page": 10,
"prev_page_url": null,
"to": 4,
"total": 4
}
对结果进行排序的最佳方法是什么? 它是在急切的负载中还是在获取结果之后? 谢谢。
尝试这个
DeliveryDays::with(['deliverySub' => function($query){
$query->orderBy('first_delivery_date');
}, 'deliverySub.delivery.location'])->paginate(10);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.