Php 订单嵌套式急切加载雄辩的Laravel

Php 订单嵌套式急切加载雄辩的Laravel,php,laravel,eloquent,Php,Laravel,Eloquent,我对雄辩的质询有意见。我正在使用渴望加载(一对多多态关系)来排序“historyable.date”,JSON如下 [ { "product_id":12, "product_name": "Product C", "historyable_type":"App\\Delivery", "historyable":{ "id":2, "date":"0303\/0404\/2020", "

我对雄辩的质询有意见。我正在使用渴望加载(一对多多态关系)来排序“historyable.date”,JSON如下

[
   {
      "product_id":12,
      "product_name": "Product C",
      "historyable_type":"App\\Delivery",
      "historyable":{
         "id":2,
         "date":"0303\/0404\/2020",
         "for":"Customer A",
         "created_at":"2020-04-02T09:46:48.000000Z",
      }
   },
   {
      "product_id":1,
      "product_name": "Product A",
      "historyable_type":"App\\Transfer",
      "historyable":{
         "id":1,
         "date":"0202\/0404\/2020",
         "for":"First Balance ****",
         "created_at":"2020-04-02T07:30:45.000000Z",
      }
   },
   {
      "product_id":11,
      "product_name": "Product B",
      "historyable_type":"App\\Delivery",
      "historyable":{
         "id":2,
         "date":"0303\/0404\/2020",
         "for":"Customer B",
         "created_at":"2020-04-02T09:46:48.000000Z",
      }
   }
]
我试着得到这样的结果

[
   {
      "product_id":1,
      "product_name": "Product A",
   },
   {
      "product_id":12,
      "product_name": "Product C",
   },
   {
      "product_id":11,
      "product_name": "Product B",
   }
]
Products Table
#id
#name

Histories Table
#product_id
#product_name
#historyable_id
#historyable_type

Deliveries Table
#id
#date
#for

Transfers Table
#id
#date
#for
可以使用嵌套的Laravel运行吗

产品模型

public function track() {
   return $this->hasMany('App\History');
}
历史模型

public function historyable(){
   return $this->morphTo()->orderBy('date', 'desc');
}
产品控制器

public function json_history(Product $product) {
   $data = $product->with('track.historyable')
   ->where('id', $product->id)
   ->first();
   return $data->track;
}
我有这样的数据库

[
   {
      "product_id":1,
      "product_name": "Product A",
   },
   {
      "product_id":12,
      "product_name": "Product C",
   },
   {
      "product_id":11,
      "product_name": "Product B",
   }
]
Products Table
#id
#name

Histories Table
#product_id
#product_name
#historyable_id
#historyable_type

Deliveries Table
#id
#date
#for

Transfers Table
#id
#date
#for
我不知道,但它对我有用。。但是我还是找到了一条比较短的路


我不知道,但它对我有用。。但是我还是找到了更简短的方法..

请发布您的代码。发布您的代码以便我们帮助您。谢谢您,我刚刚添加了我的代码..请发布您的代码。发布您的代码以便我们帮助您。谢谢您,我刚刚添加了我的代码。。