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

untymage's avatar

Check if the route have any query string?

I want to check if the current route have any query string on it no matter the key or value, Which method should i use in $request instance?

0 likes
10 replies
jlrdw's avatar

Laravel has a $request->has method. It can be found in the request chapter.

1 like
untymage's avatar

that method only check one key, I'm looking for something that check if query string applied to the route (no specific key value matter)

untymage's avatar

@sinnbeck thanks, That' a hacky way, Do you think it worth if i apply a macro method in Request class ? It would be nice if the method returns boolean

Sinnbeck's avatar

Well the clean way is doing something like in the link by @jlrdw

Checking the content of $_SERVER['QUERY_STRING'] might work

babusharif's avatar

if will be better if laravel has any method on it rather than using $_SERVER

thinkverse's avatar
Level 15

You can access the ParameterBag that holds the query parameters from the Request, then use its count method.

use Illuminate\Http\Request;

Route::get('/', function (Request $request) {
  return $request->query->count();
});

No need to complicate it by checking $_SERVER['QUERY_STRING'] or $_GET when Symfonys' ParameterBag and Laravel handle it for you. 🙂

3 likes
Sinnbeck's avatar

@thinkverse only two years too late. But I agree with your way of doing it. It's how I would do it with my current knowledge :)

1 like
thinkverse's avatar

@Sinnbeck better late than never right. Hopefully, it might help someone who is looking to do the same thing. 🙂

1 like

Please or to participate in this conversation.