I'm working on a feature that is supposed to pull in the top 8 most viewed posts from a database. The code I'm using isn't quite returning what I need and I'm not sure why this is happening. My database row name is views
. The code I'm using is below:
$most_populars = \App\Post::where('status', '=', 'PUBLISHED')->get()->sortByDesc('views');
dd($most_populars);
When I use this, I get an array of 123 posts back which is what I expect. So next I use a foreach loop to get the views like this:
foreach ($most_populars as $most_popular) {
dd($most_popular->views);
}
When I use the dd() inside of the foreach loop, I get the post with the most views, but I only get the first one. Why is this? Shouldn't I be seeing the view count for 123 other posts? Any help I can get is a great help =) Thanks guys.
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2fJr0mu
via IFTTT
Aucun commentaire:
Enregistrer un commentaire