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

hjortur17's avatar

Clear query string

Hi, can anyone assist me clearing query strings when it's empty? I'm doing search and when the input is empty I want the query string to be removed, but at the moment it's like this: ?leita=

Here is what I have:

Index.vue

search: throttle(function (value) {
            this.$inertia.get('/bloggið', {leita: value}, {
                preserveState: true,
                replace: true
            });
        }, 500)
0 likes
2 replies
tykus's avatar
tykus
Best Answer
Level 104

If there were multiple key/values in the object I would suggest:

Object.entries(obj).reduce((a,[k,v]) => (v ? (a[k]=v, a) : a), {})

However, since there is only one:

search: throttle(function (value) {
  this.$inertia.get('/bloggið', (value ? {leita: value} : {}), {
    preserveState: true,
    replace: true
  });
}, 500)
3 likes
hjortur17's avatar

@tykus - Thanks for this. Do you have idea of how I can create a method to clear the query string?

Please or to participate in this conversation.