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

mmichaelbiz's avatar

How can we parse repeated URL params in Laravel?

We can pick up url params with something like Request::input('q').

So making a request like this can be handled nicely: GET https://www.myapi.com/api/v1/entity?q=param1

But how can we handle the following? (Note that there are two 'q' params): GET https://www.myapi.com/api/v1/entity?q=param1&q=param2

I would have expected Request::input('q') to return an array in this case with the multiple values, however it only returns the last value (i.e. param2).

I know we can handle multiple values on a single param with a comma separator for example: GET https://www.myapi.com/api/v1/entity?q=param1,param2

However I would also like to handle repeat multiple params. Below is a real world example from the google translate api using repeat params:

GET https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff

Any insights much appreciated!

0 likes
13 replies
toniperic's avatar

You cannot. It's the same as doing the following in PHP

$var = 'foo';
$var = 'bar';

echo $var; // returns 'bar', and there's no way you can go through its history, so to speak
1 like
bashy's avatar

What about using arrays in the query string?

?foo[0]=bar&foo[1]=baz
2 likes
mmichaelbiz's avatar

@toniperic, thank you for the reply. I understand the problem but was wondering if there is a Laravel method I didn't know.

@bashy, yeah that does indeed work. There might be cases when you might want to specify the array key I suppose so this method opens up some other nice usages but not exactly what I had in mind.

Would be nice not to have to specify the array key but can def use this as a workaround for the time being. Thank you!

I'll look in to suggesting this as an improvement.

londoh's avatar

as @toniperic points out same name query params will be overwritten as laravel request params

but you can still get the original query string from the laravel request object and do whatever you want with it

$queryString = $request->server->get('QUERY_STRING')); // untested but something like that
// and parse away to your hearts content
// usual overides about being careful with raw user input etc

regards

l.

bashy's avatar

I don't really understand what you want to do but using an array (as I posted) is better than getting an array from the request then having the exact same array keys anyway?

Where are you using this q=&q=?

mmichaelbiz's avatar

I was looking to replicate the convention used by the guys at google translate. (See example in my original post.)

For example it could be used to build up a search url. You can keep tagging on additional params as you need to without worrying about the array key. The back end can then squash these down in to an array that you can iterate through to return filtered data.

Graham Campbell seems to feel it is a bad idea in any case and advised to avoid it.

Its's not a problem. I can handle it in other ways.

bashy's avatar

Build a helper function for it? I don't see what the problem is.

Here's a good explanation of the problem with using PHP. Google obviously doesn't use PHP and can handle multiples as an array https://php.net/manual/en/function.parse-str.php#76792

$query  = explode('&', $_SERVER['QUERY_STRING']);
$params = array();

foreach($query as $param)
{
    list($name, $value) = explode('=', $param);
    $params[urldecode($name)][] = urldecode($value);
}

Ref: http://stackoverflow.com/questions/353379/how-to-get-multiple-parameters-with-same-name-from-a-url-in-php

3 likes
mmichaelbiz's avatar

Thanks @bashy. As I mentioned earlier there is no problem! There are many ways to work around it. The one you sent through is a great example, thanks for that!

My initial question was if something like this helper already exists in Laravel, similar to Request::Input(), as it seemed to me a common use case. As we have established, it does not.

Hopefully it will be added in the future. Perhaps in the form of Request::multiple('q') as suggested by @franzliedke.

bashy's avatar

No problem.

I'd suggest doing a pull request to add it in then. Probably won't get added unless you do :P

lostdreamer_nl's avatar

As I see it, the problem lies here:

 as it seemed to me a common use case. As we have established, it does not.

As there is no HTTP spec about how to handle duplicate values in POST or GET, PHP chose to go with "the last one wins".

So just like all other variable in PHP, once a variable is overridden, you cannot get it back.

This makes the whole 'duplicate keys in GET' a very uncommon usecase in the PHP world and as such I dont think any framework will give helpers out of the box to do this.

But, as Bashi showed, it's very easely implemented yourself if you really want to, but as all others have tried to say: go for the q[]=....&q[]=..... route, it's an accepted standard.

Please or to participate in this conversation.