Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 1 year ago.
0

I am doing it this way

Post model

public function comments3() {
   return $this->morphMany('Comment','commentable')->limit(3)->orderBy('created_at','desc');
}

And view

@foreach ($post->comments3->reverse() as $comment)
   //code
@endforeach

I am using polymoprhic relations as another models can have comment too. In your case just switch morphMany to hasMany

Last updated 9 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Seshiro seshiro Joined 26 Jan 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.