Laravel:雄辩的orderBy hasOne关系列与with一起使用

我有一个Orders有hasOne关系的模型participant。


public function participant()

{

    return $this->hasOne('App\OrderParticipant', 'order_id');

}

我需要检索Orders按以下排序的集合participant.last_name


我的方法


$orders = \App\Orders::with(['participant',])

              ->where('user_id', $user->id)  

              ->orderBy('participant.last_name')

              ->get();

失败:


未定义的表:7错误:缺少表\“参与者\” \ nLINE的FROM子句条目:... 1


收集后我试图对它进行排序


return $orders->sortBy('participant.last_name');

但这根本没有排序


顺便说一句我正在使用postgres


扬帆大鱼
浏览 470回答 3
3回答

互换的青春

您可以通过实现  //  eager loading  $orders = \App\Orders::with(['participant'=> function($q){             $q->orderBy('last_name', 'asc/desc');            }           ])->get();
打开App,查看更多内容
随时随地看视频慕课网APP