Giter VIP home page Giter VIP logo

query-url-builder's Introduction

Easy query url building

Packagist Version License: MIT Test

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.

Installation

$ composer require forrestedw/query-url-builder

Basic usage

For greatest convenience, use it from the facade.

use Forrestedw\QueryUrlBuilder\QueryUrl;

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

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

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

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

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

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

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

Filter

Check if a filter is set

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

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

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

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

// 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

// 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()

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

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

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

    <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.

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

    <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

Blade components

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.


query-url-builder's People

Contributors

forrestedw avatar innoflash avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

innoflash

query-url-builder's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.