I want to get previous and next news order by date. The code below works fine if there is only one news a day. But cannot handle multiple news on the same day.
NewsController.php
public function detail($slug){
$news = AppNews::active()->where('slug', $slug)->firstOrFail();
$prev_news = AppNews::whereDate('date', '>', $news->date)->active()->orderBy('date', 'desc')->first();
$next_news = AppNews::whereDate('date', '<', $news->date)->active()->orderBy('date', 'desc')->first();
}
web.php
Route::get('/news/{slug}', 'NewsController@detail')->name('news');
Thanks
2
Answers
I found a solution:
you used ->first();
this only returns 1 value. so change it up to get()