I'm new in laravel. I need to have in my homepageBlog fetch 3 posts for each categories and just show 4 categories.
I'm doing in my HomeController
$categories = Category::with(['posts' => function($query){
$query->orderBy('published_at', 'DESC')->take(3)->get();
}])->take(4)->get();
In my model
//post models
public function category(){
return $this->belongsToMany('Category', 'post_categories', 'post_id', 'category_id');
}
in my category model
public function posts(){
return $this->hasMany('Post');
}
But when I'm going in my view it's just showing 3 latest post only for 1 category and show 1 post for another.