1

Hi I have two models Category,News, i want to retrieve categories with 3 latest news in each category, News models have created_at column for date when it created.

Can i achieve this in laravel

Raza Mohd
  • 25
  • 3
  • Possible duplicate of [I'm trying to load only the last 3 comments on every post](https://stackoverflow.com/questions/47505673/im-trying-to-load-only-the-last-3-comments-on-every-post) – M Khalid Junaid Jul 19 '18 at 07:06

1 Answers1

1

try this way,

 $news = Category::whereHas('news',function($query){
    $query->orderBy('created_at', 'desc')->take(3);
})->get();
Mohamed Akram
  • 2,244
  • 15
  • 23