3
我有一个这样的雄辩查询:Laravel - ORDERBY嵌套关系
Forum::with(['comments.user'])->find($id);
这将返回一个嵌套的结果forum -> its comments -> user who commented
。
我如何在comments
表上申请orderBy()
?
我有一个这样的雄辩查询:Laravel - ORDERBY嵌套关系
Forum::with(['comments.user'])->find($id);
这将返回一个嵌套的结果forum -> its comments -> user who commented
。
我如何在comments
表上申请orderBy()
?
您可以调用with()
时进一步查询元素添加到预先加载查询阵列内通过封闭:
Forum::with([
'comments' => function($q){
$q->orderBy('created_at', 'desc');
},
'comments.user'
])->find($id);
现在你要指定comments
和comments.user
但不用担心它会不会运行更多查询,而不仅仅是comments.user
。