0

I am retrieving comments related to my batsmans profile on the view through relationships. Is their anyway that i could order by the latest comment through this or will i have to do it in the controller?

current retrieval of comments.

@foreach($batsmen->comments as $comments)
<p> $comments->comment</p>
<p> Posted by: $comments->user_id</p>
@endforeach
dionarap
  • 51
  • 5
  • 1
    [It can be done on the model where the relation is defined](https://stackoverflow.com/questions/18143061/laravel-orderby-on-a-relationship) – Quezler Sep 05 '17 at 15:59

1 Answers1

0

You can do this if it's an eloquent object.

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