Rushy Rian
Rushy Rian

Reputation: 37

Laravel Limit Related Model

How to Limit Related Model from Parent Model in Laravel,

I have Category and News Model, I want to fetch all categories and each category with 3 latest news.

Upvotes: 1

Views: 41

Answers (1)

Mohamed Akram
Mohamed Akram

Reputation: 2117

Please try this. You will get all categories that as minimum one news model related

$news = Category::whereHas('news',function($query){
    $query->orderBy('created_at', 'desc')->take(3);
})->get();

Upvotes: 2

Related Questions