Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rezuankassim's avatar

Laravel query builder

Hi, currently I have a query that queried out the results of top 10 screen visited, and the results is such as below

screens        | visits
Home           | 100
Dashboard      | 25
Products       | 30
...

Is there any way that I can get a new row that is for Others which is the count of all visits for the page that is not in the top 10?

screens        | visits
Home           | 100
Dashboard      | 25
Products       | 30
...
Others         | 400

Thank you in advance! 👊🏻

0 likes
1 reply
bobbybouwmann's avatar
Level 88

I'm not sure if you can even do it in a query. You can probably do a subquery where you do a count based on the items that are not in your previous list. I personally never created a query like that.

I would handle this differently by simply querying the top 10 and also querying the count of the total items. Then you can use the collection methods to calculate the correct number of results

$totalScreens = Screens::count();
$screens = Screens::orderBy('visits')->limit(10)->get();

$others = $totalScreens - $screens->sum('visits');

Please or to participate in this conversation.