Download the PHP package nycu-csit/laravel-query-builder without Composer
On this page you can find all versions of the php package nycu-csit/laravel-query-builder. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download nycu-csit/laravel-query-builder
More information about nycu-csit/laravel-query-builder
Files in nycu-csit/laravel-query-builder
Package laravel-query-builder
Short Description Build query by query string
License MIT
Informations about the package laravel-query-builder
Laravel Query Builder
This package makes you to filter, sort, include, count eloquent model easily based on the query string from the request.
QueryBuilder
NycuCsit\LaravelQueryBuilder\QueryBuilder extends laravel query builder and supports fluent interface, you can use any
laravel query builder function call on it.
Filtering
The filter query parameters can be used to add where clauses to your Eloquent query.
Only allowed query parameters could be parsed and built as a SQL query. If the query parameters is not allowed,
it will be skipped without any error.
The supported format of query string:
filter[<parameter>][<operator>]=<value>is JSON:API format, this format is strongly recommended.filter[<parameter>]=<value>is JSON:API format<parameter>[<operator>]=<value>is seen as a filter<parameter>=<value>is seen as a filter
Use QueryBuilder to parse query string and build query:
The SQL query of GET /users?year[gt]=2022 will be look like this:
Add many allowed query parameter filters:
Use add() to add allowed query parameter filter:
Operators
| Operator | Meaning | Value | String | Number | Boolean | Datetime | Date | Time |
|---|---|---|---|---|---|---|---|---|
eq |
Equals to | Any | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
neq |
Not equals to | Any | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
gt |
Greater than | Any | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
lt |
Less then | Any | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
ge |
Greater or equals to | Any | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
le |
Less or equals to | Any | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
in |
Equals to one of array | Comma-seperated value | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
notin |
Not equals to one of array | Comma-seperated value | ✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
is |
Is | null or notnull |
✅ | ✅ | ✅ | ✅ | ✅ | ✅ |
like |
Like (SQL fuzzy string) | string | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
year_eq |
Year equals to | number | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
month_eq |
Month equals to | number | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
day_eq |
Day equals to | number | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
date_eq |
Date equals to | date string (RFC3339) | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
time_eq |
Date equals to | time string (RFC3339) | ❌ | ❌ | ❌ | ✅ | ✅ | ✅ |
If there aren't a specified [operator] , the query builder will use the default operator you specified.
By default, the operator is eq.
Query Scope
The scope query parameters can be used to apply a
Query Scope to your Eloquent query. Only allowed scope could be
parsed and built as a SQL query. If the scope is not allowed, it will be skipped without any error.
Supports these format of query string:
scope[<scopeName>]=<toggle>- Toggle the scope
<scopeName>is the name of your scope, it is camelCase format<toggle>boolean value, specify whether to apply this scope
scope[<scopeName>][<scopeArgumentName>]=<scopeArgumentValue>- For dynamic scope
<scopeName>is the name of your scope, it is camelCase format<scopeArgumentName>is the argument name to be passed to the scope<scopeArgumentValue>is the argument value to be passed to the scope
For example, you may define a scope function:
Use allowedScopes() in the query builder:
The query string
will be applied as $query->banned().
The arguments will be passed by corresponding names, the order does not affect. If the arguments doesn't fit the scope function's definition, this scope query string will be ignored.
Argument Transformer
You may check or modify the arguments array in the transformer. The transformer could be an array of callables or single callable, the array transformer will be invoked one by one.
The scope query builder will send the arguments in the query string of the scope to the transformers.
Here is the query string:
the arguments of scope 'ofCondition' is:
You may use Cast as transformer to cast string to other type in the arguments:
Or, you can just use a closure as transformer:
Filtering Existence
The filter query parameters with existence operators can be used to add
has() clauses to your Eloquent
query.
The not allowed query parameters will be skipped without any error.
The supported format of query string:
filter[<parameter>][<operator>]=<value>is JSON:API format
Use QueryBuilder to parse query string and build query:
The SQL query of GET /users?filter[posts][exist]=true will be look like this:
Add many allowed query parameter existence filters:
Use add() to add allowed query parameter existence filter:
Operators
| Operator | Meaning | Value |
|---|---|---|
exist |
Have at least one related record | bool |
exist_eq |
Number of related records equals to | number |
exist_neq |
Number of related records not equals to | number |
exist_gt |
Number of related records greater than | number |
exist_lt |
Number of related records less then | number |
exist_ge |
Number of related records greater or equals to | number |
exist_le |
Number of related records less or equals to | number |
Including relationships
The include query parameter will load any Eloquent relation or relation count on the resulting models. All includes
must be explicitly allowed using allowedIncludes().
The supported format of query string:
include=<relation1>,<relation2>,...is JSON:API format<parameter>[include]=<value>\<value> is boolean
Use QueryBuilder to parse query string and build query:
The SQL query of GET /users?include=posts will be look like this:
Be caution, you should call allowedIncludes or add(AllowedIncludes...) only once per query, because including
validation will be added in future version.
The corresponding shorthand for add() is NycuCsit\LaravelQueryBuilder\Criteria\AllowedIncludes.
Sorting
The sort query parameter is used to determine by which property the results' collection will be ordered. Sorting is
ascending by default and can be reversed by adding a hyphen (-) to the start of the property name.
If there is any not allowed parameter in sort query parameter, an Illuminate\Validation\ValidationException
will be thrown.
The supported format of query string:
sort=<parameter1>,<parameter2>,...is JSON:API format
Use QueryBuilder to parse query string and build query:
The SQL query of GET /users?sort=create_time,-email will be look like this:
The corresponding shorthand for add() is NycuCsit\LaravelQueryBuilder\Criteria\AllowedSorts.
Counting relationship
The count query parameter will count on the related records. All counts must be explicitly allowed using allowedCounts().
The supported format of query string:
count=<relation1>,<relation2>,...is JSON:API format<parameter>[count]=<value>\<value> is boolean
Use QueryBuilder to parse query string and build query:
The SQL query of GET /users?count=posts will be look like this:
As you can see, there is a new field news_posts_count is appended, but the field name will be<relationName>_count instead
of <parameterName>_count.
The corresponding shorthand for add() is NycuCsit\LaravelQueryBuilder\Criteria\AllowedCounts.
Extra Queries for single Model
Eager Loading
Sometimes, we want to load relationships for a model by query string. Just use HasExtraQuery trait in your model class:
The supported format of query string:
load[<parameterName>]=<value>is JSON:API format<parameterName>the name which you want to eager load<value>boolean value, toggle the eager loading
Now, you can use allowedLoad() function on your model:
The query string &load[author]=1 will be applied as $post->load('user').
The allowedLoad() function supports fluent interface.