Laravel - Eloquent 与 Order By Inner 查询的模型关系不起作用

Laravel - Eloquent Model Relationship with Order By Inner query is not working

这是我的 collection。

0 => array:11 [
"_id" => "5d6447ae1ab38901a80c7062"

"order" => 4 //order of out array

"widget_order" => array:6 [
  "_id" => "5d6451281ab38901b4689d24"
  "widget_id" => "5d6447ae1ab38901a80c7062"
  "order" => 3 //order of inner array 
  "user_id" => "5cc211d488737704e876d892"

]
 ]
 1 => array:11 [
"_id" => "5d6447c81ab389ff5c5c37a2"

"order" => 3 //order of out array

"widget_order" => array:6 [
  "_id" => "5d6451281ab38901b4689d25"
  "widget_id" => "5d6447c81ab389ff5c5c37a2"
  "order" => 4 //order of inner array
  "user_id" => "5cc211d488737704e876d892"

]
]

现在这是查询的结果。

$allWidgets = Widget::with(['widgetOrder'])->orderBy('order', 'desc')->get();

现在我想根据 widget_order.order

对它进行排序

我正在尝试这个,但它不起作用。

$allWidgets = Widget::with(['widgetOrder' => function ($query) {
        $query->orderBy('widget_order.order', 'desc');
    }])->get();

你可以试试这个

 $allWidgets = Widget::with('widgetOrder')->orderBy('widgetOrder.order', 'desc')->get();