PHPackages                             forrestedw/query-url-builder - 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. forrestedw/query-url-builder

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

forrestedw/query-url-builder
============================

A one class package to simplify manipulating front end query urls

1.0.0(6y ago)15871MITPHPCI failing

Since May 8Pushed 4y ago1 watchersCompare

[ Source](https://github.com/forrestedw/query-url-builder)[ Packagist](https://packagist.org/packages/forrestedw/query-url-builder)[ RSS](/packages/forrestedw-query-url-builder/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (9)DependenciesVersions (14)Used By (0)

Easy query url building
=======================

[](#easy-query-url-building)

[![Packagist Version](https://camo.githubusercontent.com/0d2d624feae1cf42ca253d19b864b797ad2642905dbd49d157b0c8e9dfb6e7e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f72726573746564772f71756572792d75726c2d6275696c646572)](https://camo.githubusercontent.com/0d2d624feae1cf42ca253d19b864b797ad2642905dbd49d157b0c8e9dfb6e7e3/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f666f72726573746564772f71756572792d75726c2d6275696c646572) [![License: MIT](https://camo.githubusercontent.com/fdf2982b9f5d7489dcf44570e714e3a15fce6253e0cc6b5aa61a075aac2ff71b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d79656c6c6f772e737667)](https://opensource.org/licenses/MIT) [![Test](https://github.com/forrestedw/query-url-builder/workflows/Test/badge.svg)](https://github.com/forrestedw/query-url-builder/workflows/Test/badge.svg)

This packages makes it easy to make the links necessary for use in the front en with [spatie/laravel-query-builder](https://docs.spatie.be/laravel-query-builder). The package is amazing helpful for the back end, and the front end is outside of the scope of the project. Creating the links for the front end can be verbose. This package makes it easy.

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

[](#installation)

```
$ composer require forrestedw/query-url-builder
```

Basic usage
-----------

[](#basic-usage)

For greatest convenience, use it from the facade.

```
use Forrestedw\QueryUrlBuilder\QueryUrl;
```

### Sort

[](#sort)

#### Set a sort

[](#set-a-sort)

```
QueryUrl::sortBy('name')->build(); // http://example.test/?sort=name, ie name ASC

QueryUrl::sortBy('-name')->build(); // http://example.test/?sort=-name, ie name DESC
```

#### Access the sort

[](#access-the-sort)

```
// On page  http://example.test/?sort=name

QueryUrl::sort === 'name' // true

QueryUrl::sort === '-name' // false
```

#### Reverse the sort

[](#reverse-the-sort)

```
// On page http://example.test/?sort=name

QueryUrl::reverseSort()->build(); // http://example.test/?sort=-name, ie ASC goes to DESC

// On page http://example.test/?sort=-name

QueryUrl::reverseSort()->build(); // http://example.test/?sort=name, ie DESC goes to ASC
```

#### Remove a sort

[](#remove-a-sort)

```
// On page http://example.test/?sort=name

QueryUrl::removeSort()->build(); // http://example.test/
```

### Filter

[](#filter)

#### Check if a filter is set

[](#check-if-a-filter-is-set)

```
// On page http://example.test/?filter[name]=Joe

QueryUrl::hasFilter('name') // true

QueryUrl::hasFilter('email') // false
```

#### Set filters

[](#set-filters)

```
QueryUrl::setFilter('active', true)->build() // http://example.test/?filter[active]=1

QueryUrl::setFilter('active', false)->build() // http://example.test/?filter[active]=0

QueryUrl::setFilter('active', true)->setFilter('valid', false)->setFilter('name','John')->build() // returns http://example.test/?filter[active]=1&filter[valid]=0&filter[name]=John
```

Filters can also be set using an associative array:

```
$filters = [
    'active' => false,
    'valid' => true,
];

QueryUrl::setFilters($filters)->build() // http://example.test/?filter[active]=0&filter[valid]=1
```

#### Remove a filter

[](#remove-a-filter)

```
// On page http://example.test/?filter[active]=1&filter[valid]=0&filter[name]=John

QueryUrl::removeFilter('active')->build(); // http://example.test/?&filter[valid]=0&filter[name]=John
```

#### Combine various `sort` and `filter` options

[](#combine-various-sort-and-filter-options)

```
// On page http://example.test/?filter[active]=1&filter[valid]=0&filter[name]=John

QueryUrl::add('active', true)->sortBy('-email')->build(); // http://example.test/?&filter[active]=1&sort=-email, ie active users sorted by email DESC
```

#### forUrl()

[](#forurl)

By default, `QueryUrl` returns the new query params for the route you are already on:

```
// On page http://example.test/

QueryUrl::setFilter('someFilter',true)->build(); // http://example.test/?filter=[someFilter]=1
```

If you need a different url, use `forUrl()`. It accepts plain urls or named routes:

```
// On page http://example.test/

QueryUrl::forUrl('this/then/that/')->setFilter('someFilter',true)->build(); // http://example.com/this/then/that?filter=[someFilter]=1

QueryUrl::forUrl('project.show', ['project_id' => 1])->setFilter('someFilter',true)->build(); // http://example.test/projects/1?filter=[someFilter]=1
```

---

### Using in blade

[](#using-in-blade)

Use the `queryUrl()` in your blade files like below.

#### Sorting

[](#sorting)

The following example will create a link that cycles through three states of being sorted:

1. Sorted A-Z
2. Sorted Z-A
3. Unsorted.

```
@if(QueryUrl::getSort() === 'name')

    Name - showing A-Z

@elseif(QueryUrl::getSort() === '-name')

    Name - showing Z-A

@else

    Name - showing unsorted

@endif
```

The url text shows what sort the user will currently be seeing. The link will take the user to the next sort state.

#### Filtering

[](#filtering)

A similar approach is taken for boolean value filtering, and cycling through the three states:

1. Show `true` only
2. Show `false` only
3. Show all

```
@if(! QueryUrl::hasFilter('active'))

    Active - all

@elseif(QueryUrl::filter('active') === true)

    Active // currently showing true only

@else

    Active  // currently showing true only

@endif
```

#### Blade components

[](#blade-components)

For ease, the two above trios of if-else links can be outputted using the following, respectively:

```

```

Behind the scenes the `sort` or `filter` attribute is handled to snake case it for the attribute in question. For example, the sort example displays `First Name` (exactly as passed) but sorts for `first_name`.

---

###  Health Score

30

—

LowBetter than 62% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity16

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 76.1% of commits — single point of failure

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 ~56 days

Recently: every ~122 days

Total

13

Last Release

1563d ago

PHP version history (2 changes)0.2.0PHP ^7.1

0.4.0PHP ^7.1|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/c5f440bd7200a290b1766fe8f9879079de85162acf118f34838b8ac9bee4725a?d=identicon)[forrestedw](/maintainers/forrestedw)

---

Top Contributors

[![forrestedw](https://avatars.githubusercontent.com/u/36917362?v=4)](https://github.com/forrestedw "forrestedw (51 commits)")[![innoflash](https://avatars.githubusercontent.com/u/12772919?v=4)](https://github.com/innoflash "innoflash (16 commits)")

### Embed Badge

![Health badge](/badges/forrestedw-query-url-builder/health.svg)

```
[![Health](https://phpackages.com/badges/forrestedw-query-url-builder/health.svg)](https://phpackages.com/packages/forrestedw-query-url-builder)
```

###  Alternatives

[spatie/query-string

Manipulate query strings

161791.0k3](/packages/spatie-query-string)[frosh/shopware-rector

Shopware specific Rector

47432.8k5](/packages/frosh-shopware-rector)[flarum/likes

Allow users to like posts.

17461.4k26](/packages/flarum-likes)

PHPackages © 2026

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