Download the PHP package youcanshop/queryoption without Composer
On this page you can find all versions of the php package youcanshop/queryoption. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download youcanshop/queryoption
More information about youcanshop/queryoption
Files in youcanshop/queryoption
Package queryoption
Short Description Package to help with filtering/search/sort in queries
License MIT
Informations about the package queryoption
QueryOption
This package helps you manipulate HTTP query data as an object instead of passing an array through different layers of your application.
Usage
Inside a controller we tend to extract the params from the request and send them to our service and then to the repository to perform search, sort or filtering.
From the example above, the QueryOptionFactory
helps create the QueryOption
object that hold values needed for search, sort and filters.
Since in this example we have a Laravel application, we can use a specific factory method to crate directly from Request
object.
createFromGlobals() |
create the QueryOption object from the global $_REQUEST object. |
createFromArray(array $attributes) |
create the QueryOption object from an array passed in param. |
createFromIlluminateRequest(Request $request) |
create the QueryOption object from a Laravel Illuminate request object. |
createFromSymfonyRequest(Request $request) |
create the QueryOption object from a Symfony HTTP foundation request object. |
URL Params
Since the QueryOption package parse the URL parameters, we're going to explain the param names below:
q |
Used for search |
search_type |
(like : default, equal ) |
page |
Current page (when working with pagination) |
limit |
Limit query result (when working with pagination) |
sort_field |
Name of the field when sorting (created_at : default) |
sort_order |
Sorting direction. (asc , desc : default) |
filters |
An array of filters (as described below) |
field |
Name of the field to filter by |
operator |
Comparison operator (= : default, != , > , < , => , <= , is , is_not , in ) |
value |
The value to compare by |
Query Option
The QueryOption
is the glue that holds all the rest of the components.
For most applications, each controller has a set of filters that are allowed in certain context (admin vs normal user).
In this case, you can use the allowedFilters()
method inside the controller to limit passing filters in the wrong context.
An example would be listing blog posts. The admin can all the posts, while the normal user can only see the published ones.
Laravel Bridge
For Laravel applications, you can add Query Option provider inside config/app.php
After that you can benefit from helpers like:
Inside your repository you can use what we call criterias. Here's an example on how it works:
So calling the paginate()
method will pass the query through the list of criterias to add the necessary logic for each defined query option.
Then, we return the modified query instance to continue the pagination.
Below is the code inside each criteria to illustrate how it works.
The search criteria do a search using like
or equal
using the term when it's not empty, and return the $query
and $queryOption
for the next criteria.
The sorting is pretty straightforward in this example. An important thing is to guard against sorting using not allowed fields.
This is it, the pagination will take in consideration the fitering by publish date, searching by title and sorting by publish date.