I have following function in Category model:
public function news(){
return $this->belongsToMany(News::class,'news_categories','category_id','news_id');
}
public function writer(){
return $this->belongsToMany(NewsAuthor::class,'news_authors','news_id','user_id');
}
public static function getNewsByCategory($id, $limit){
return Category::where('id', $id)->has('news')
->with(['news' => function($q) use(&$limit) {
$q->latest()->limit($limit);
}])->first();
}
to fetch news by Category id, now, I also want to fetch news writer which is in the news_author table.
How can I get news author with my function getNewsByCategory
?