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

tvbz's avatar
Level 4

Query string array format best practice?

In my livewire component I have an certain amount of features to filter on. If i store the features in an array I get an ugly query string like: ?features[0]=firstfeature&features[1]=secondfeature&features[2]=thirdfeature&...

I don't like this because it can get very long very fast and also it's not as readable as for example: ?features=firstfeature,secondfeature,thirdfeature,...

How to work around this and make transform the array in to a nice querystring?

For now, I am working around it using Vanilla JS and a decicated hidden field in the blade view. When you click a feature checkbox, in JS split the field value to an array, remove checkbox value if unchecked and push checkbox value if checked, merge back to string, finally dispatch the input event so Livewire can handle changes.

Is this the way to do it? Or is there a different way available in LiveWire?

My checkboxes have no wire:model=. I am just listening to changes by querySelector().

It feels a bit strange going back to Vanilla JS while inside a Livewire component. Sure it works, but I'm just using Livewire for a filter component, might as well just ajax it. :-\

Thanks!

0 likes
4 replies
automica's avatar

if its livewire, why does the query string need to be easy for a human to read?

tvbz's avatar
Level 4

@automica Because that's how I want it. :)

Not really the point here,.. in some testing I easily get to an url with 1000 chars. I still have some filters to add and I'm allready getting close to some url size restrictions. But also it's just a very ugly url to share.

If it doesn't need to be readable, I could shorten the url significantly by using feature ID's instead of feature slugs and use "f" instead of features. Then it would become something like this: ?f[0]=1&f[1]=5&f[2]=3&...

But I'm looking for the "pretty" solution first.

martinbean's avatar

@tvbz It’s a query string. It’s part of the HTTP spec. That’s how multiple values are passed for a single key. You‘re going to get long query strings no matter if you use the actual array syntax, or comma-separated values.

Just use query strings how they’re intended to be used.

jlrdw's avatar

@tvbz this forum uses query string, i.e.,

https://laracasts.com/discuss?filter_by=contributed_to&page=2

I don't think a query string is ugly. I like query strings better than:

https://laracasts.com/discuss/contributed/2

Please or to participate in this conversation.