This packages makes it easy to make the links necessary for use in the front en with spatie/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.
$ composer require forrestedw/query-url-builder
For greatest convenience, use it from the facade.
use Forrestedw\QueryUrlBuilder\QueryUrl;
QueryUrl::sortBy('name')->build(); // http://example.test/?sort=name, ie name ASC
QueryUrl::sortBy('-name')->build(); // http://example.test/?sort=-name, ie name DESC
// On page http://example.test/?sort=name
QueryUrl::sort === 'name' // true
QueryUrl::sort === '-name' // false
// 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
// On page http://example.test/?sort=name
QueryUrl::removeSort()->build(); // http://example.test/
// On page http://example.test/?filter[name]=Joe
QueryUrl::hasFilter('name') // true
QueryUrl::hasFilter('email') // false
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
// 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
// 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
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
Use the queryUrl()
in your blade files like below.
The following example will create a link that cycles through three states of being sorted:
- Sorted A-Z
- Sorted Z-A
- Unsorted.
@if(QueryUrl::getSort() === 'name')
<a href="{{ QueryUrl::reserveSort()->build() }}">Name - showing A-Z</a>
@elseif(QueryUrl::getSort() === '-name')
<a href="{{ QueryUrl::removeSort()->build() }}">Name - showing Z-A</a>
@else
<a href="{{ QueryUrl::sortBy('name')->build() }}">Name - showing unsorted</a>
@endif
The url text shows what sort the user will currently be seeing. The link will take the user to the next sort state.
A similar approach is taken for boolean value filtering, and cycling through the three states:
- Show
true
only - Show
false
only - Show all
@if(! QueryUrl::hasFilter('active'))
<a href="{{ QueryUrl::setFilter('active', true)->build() }}">Active - all</a>
@elseif(QueryUrl::filter('active') === true)
<a href="{{ QueryUrl::setFilter('active', false)->build() }}">Active</a> // currently showing true only
@else
<a href="{{ QueryUrl::removeFilter('active')->build() }}">Active</a> // currently showing true only
@endif
For ease, the two above trios of if-else links can be outputted using the following, respectively:
<x-queryUrl-sort sort="First Name"/>
<x-queryUrl-bool-filter filter="active"/>
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
.