I am new in Laravel How can I add pagination to getRelatedNews function I need to display simple "Next" and "Previous" links in pagination view, I try to use simplePaginate method I don't know where to put the function ?
public function news($id, $alias)
{
$crypto_news = CryptoNews::where('id', $id)->first();
if(isset($crypto_news->title)) {
$data = [
'crypto_news' => $crypto_news,
'crypto_related_news' => $this->getRelatedNews($id,100),
'crypto_most_read_news' => $this->getMostReadNews($id, 7, 6),
'crypto_news_from_bitcoin' => $this->getNewsFromBitcoin($id, 9)
];
return view(getCurrentTemplate() . '.pages.single_news', $data);
}
return redirect('/crypto-coins-news-headlines');
}
public function getRelatedNews($id, $limit)
{
$crypto_news = CryptoNews::take($limit)
->where('status', '=', 1)
->where('urlToImage', '!=', 'no_preview.png')
->where('urlToImage', '!=', '')
->where('lang', 'en')->where('id', '!=', $id)
->orderBy('publishedAt', 'desc')->get()->toArray();
$lang_news = [];
if(LaravelLocalization::getCurrentLocale() != 'en') {
$news_object = CryptoNews::take($limit)
->where('status', '=', 1)
->where('urlToImage', '!=', 'no_preview.png')
->where('urlToImage', '!=', '')->where('id', '!=', $id)
->where('lang', LaravelLocalization::getCurrentLocale())
->orderBy('publishedAt', 'desc');
if($news_object->count() > 0) {
$lang_news = $news_object->get()->toArray();
}
if($news_object->count() > $limit) {
$crypto_news = $lang_news;
} else {
$crypto_news = array_merge($lang_news, array_slice($crypto_news, 0, $limit-$news_object->count()));
}
}
return $crypto_news;
}