PHPackages                             spatie/query-string - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. spatie/query-string

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

spatie/query-string
===================

Manipulate query strings

1.0.3(5y ago)161757.7k—8.9%93MITPHPPHP ^7.3|^8.0CI passing

Since Feb 7Pushed 3mo ago7 watchersCompare

[ Source](https://github.com/spatie/query-string)[ Packagist](https://packagist.org/packages/spatie/query-string)[ Docs](https://github.com/spatie/query-string)[ Fund](https://spatie.be/open-source/support-us)[ RSS](/packages/spatie-query-string/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (3)

QueryString
===========

[](#querystring)

[![Latest Version on Packagist](https://camo.githubusercontent.com/83d17ca61a028e1b91d45a1e2a1288c726d83eddf971694d4b27e4fa76d74bce/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f71756572792d737472696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/query-string)[![run-tests](https://github.com/spatie/query-string/workflows/run-tests/badge.svg)](https://github.com/spatie/query-string/workflows/run-tests/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/788ed2fa174c0dae78ebbc92980a02050cc21b708880dbe46ede6d26b4f02f96/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f71756572792d737472696e672e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/query-string)

Work with query strings

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/2649d979d332e36ed3c647cecb58794ff45da1ed175477678c503d965b09d4a1/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f71756572792d737472696e672e6a70673f743d31)](https://spatie.be/github-ad-click/query-string)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/query-string
```

Usage
-----

[](#usage)

```
use Spatie\QueryString\QueryString;

$queryString = new QueryString($uri);
```

### Toggle parameters

[](#toggle-parameters)

#### A single toggle

[](#a-single-toggle)

```
# / > /?toggle

$queryString->toggle('toggle');
```

#### Toggle a value

[](#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

[](#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');
```

### Filter

[](#filter)

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');
```

### Sort

[](#sort)

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

$queryString->sort('field');
$queryString->sort('field');
$queryString->sort('field');
```

### Pagination

[](#pagination)

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

[](#other-useful-methods)

#### Base URL

[](#base-url)

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

```
$queryString->withBaseUrl('https://other.url');
```

#### Clear a parameter

[](#clear-a-parameter)

```
# /?toggle > /

$queryString->clear('toggle');
```

```
# /?single=b > /

$queryString->clear('single');
```

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

$queryString->clear('multi[]');
```

#### Active parameter or not

[](#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

[](#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.

### Testing

[](#testing)

```
composer test
```

### Changelog

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

### Security

[](#security)

If you've found a bug regarding security please mail  instead of using the issue tracker.

Postcardware
------------

[](#postcardware)

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, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Brent Roose](https://github.com/brendt)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance54

Moderate activity, may be stable

Popularity54

Moderate usage in the ecosystem

Community25

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~331 days

Total

3

Last Release

1995d ago

PHP version history (2 changes)1.0.0PHP ^7.1

1.0.3PHP ^7.3|^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![brendt](https://avatars.githubusercontent.com/u/6905297?v=4)](https://github.com/brendt "brendt (28 commits)")[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (17 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (8 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (5 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (4 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (2 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (2 commits)")[![owenvoke](https://avatars.githubusercontent.com/u/1899334?v=4)](https://github.com/owenvoke "owenvoke (1 commits)")

---

Tags

httpmanipulatephpqueryquery-stringspatie

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-query-string/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-query-string/health.svg)](https://phpackages.com/packages/spatie-query-string)
```

###  Alternatives

[spatie/laravel-package-tools

Tools for creating Laravel packages

945125.5M7.0k](/packages/spatie-laravel-package-tools)[spatie/laravel-data

Create unified resources and data transfer objects

1.8k28.9M627](/packages/spatie-laravel-data)[spatie/laravel-analytics

A Laravel package to retrieve Google Analytics data.

3.2k5.7M57](/packages/spatie-laravel-analytics)[spatie/macroable

A trait to dynamically add methods to a class

72759.6M64](/packages/spatie-macroable)[spatie/regex

A sane interface for php's built in preg\_\* functions

1.1k17.1M59](/packages/spatie-regex)[spatie/enum

PHP Enums

84529.1M68](/packages/spatie-enum)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
