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

nobodyownz's avatar

Add a Count Only when query params include "?count"

How can I add a total count on a Laravel API response only if there is a query params? example: /?count(orders)

0 likes
2 replies
SilenceBringer's avatar
Level 55

@nobodyownz for example for url /?count=orders

$query->when(request('count') == 'orders', fn ($query) => $query->withCount('orders'))
4 likes

Please or to participate in this conversation.