簡體   English   中英

Laravel Eloquent 嵌套急切加載應用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM