Manipulate query strings

1.0.1 2019-11-15 10:28 UTC

This package is auto-updated.

Last update: 2020-01-15 10:55:26 UTC


Latest Version on Packagist Build Status StyleCI Quality Score Total Downloads

Work with query strings


You can install the package via composer:

composer require spatie/query-string


use Spatie\QueryString\QueryString;

$queryString = new QueryString($uri);

Toggle parameters

A single toggle

# / > /?toggle


Toggle a value

# / > /?single=a

$queryString->toggle('single', 'a');
# /?single=a > /?single=b

$queryString->toggle('single', 'b');
# /?single=a > /?

$queryString->toggle('single', 'a');

Toggle multiple values

# / > /?multi[]=a&multi[]=b

$queryString->toggle('multi[]', 'a');
$queryString->toggle('multi[]', 'b');
# /?multi[]=a&multi[]=b > /?multi[]=a

$queryString->toggle('multi[]', 'b');


Filtering the query string will use the JSON API filter syntax.

# / > /?filter[field]=a

$queryString->filter('field', 'a');
# / > /?filter[field][]=b

$queryString->filter('field[]', 'b');


Sorting the query string will use the JSON API sort syntax. At the moment only single sorts are supported.

# / > /?sort=field > /?sort=-field > /?sort=field



There's built-in support for pagination:

$queryString->page(10); # /?page=10
$queryString->nextPage(); # /?page=11
$queryString->previousPage(); # /?page=9
$queryString->resetPage(); # /?

$queryString->isCurrentPage(1); # true

Note that changing any other value on the query string, will reset the page too.

Other useful methods

Base URL

Casting a QueryString to a string will generate the URL. You can choose to use a different base URL like so:


Clear a parameter

# /?toggle > /

# /?single=b > /

# /?multi[]=a&multi[]=b > /


Active parameter or not

# /?multi[]=a

$queryString->isActive('multi[]'); # true
$queryString->isActive('multi[]', 'a'); # true
$queryString->isActive('multi[]', 'b'); # false
# /?single=a

$queryString->isActive('single'); # true
$queryString->isActive('single', 'a'); # true
$queryString->isActive('single', 'b'); # false
# /?toggle

$queryString->isActive('toggle'); # true

Laravel support

A separate Laravel package will be added in the future. The Laravel package will use this one under the hood and implement the JSON API spec.


composer test


Please see CHANGELOG for more information on what has changed recently.


Please see CONTRIBUTING for details.


If you discover any security related issues, please email instead of using the issue tracker.


You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Samberstraat 69D, 2060 Antwerp, Belgium.

We publish all received postcards on our company website.


Support us

Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Does your business depend on our contributions? Reach out and support us on Patreon. All pledges will be dedicated to allocating workforce on maintenance and new awesome stuff.


The MIT License (MIT). Please see License File for more information.