通过关系检索订购评论

问题描述:

我正在通过关系检索与我的batsmans个人资料相关的评论。无论如何,我可以通过这个最新的评论来订购,还是我必须在控制器中做到这一点?通过关系检索订购评论

当前检索评论。

@foreach($batsmen->comments as $comments) 
<p> $comments->comment</p> 
<p> Posted by: $comments->user_id</p> 
@endforeach 
+1

[它可以在定义关系的模型上完成](https://*.com/questions/18143061/laravel-orderby-on-a-relationship) – Quezler

如果它是一个雄辩的对象,你可以这样做。

@foreach($batsmen->comments->orderBy('id', 'DESC')->get() as $comments) 
<p> $comments->comment</p> 
<p> Posted by: $comments->user_id</p> 
@endforeach