Reputation: 1005
I'm creating a query that gives me the top 5 countries with more visits, but my query isn't working.
My query I can limit, how would I count all record by country, to see the 5 countries more visited.
My query:
$top5 = DB::table('visits') ->select('ip','country', 'browser') ->groupBy('ip') ->get();
output example: http://pastebin.com/wtu8CnL8
Upvotes: 0
Views: 164
Reputation: 23011
First you need to group your query by country, since that's what you're looking at. Then you need to count your results, order it by that number, and just take what you need.
$top5 = DB::table('visits')
->select('ip','country', 'browser', DB::raw("count(*) as total_visits"))
->groupBy('country')
->orderBy('total_visits','DESC')
->take(5);
Upvotes: 1