Download the PHP package faridlab/laravel-grammatical-query without Composer
On this page you can find all versions of the php package faridlab/laravel-grammatical-query. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download faridlab/laravel-grammatical-query
More information about faridlab/laravel-grammatical-query
Files in faridlab/laravel-grammatical-query
Package laravel-grammatical-query
Short Description Search Query - Laravel package for filtering queries based on url query string parameters
License MIT
Homepage https://github.com/faridlab/laravel-search-query
Informations about the package laravel-grammatical-query
Laravel Filter Query String
Filter your queries based on url query string parameters like a breeze.
Compatible with Laravel 5.x 6.x 7.x 8.x.
Table of Content
- Describing the Problem
- Usage
- Installation
- Available Filters
- Sort
- Comparisons
- In
- Like
- Where clause
- Fields
- Search
- Page
- Limit
- Relationship
- Count
- With Trashed
- Order By
- Where
- Or Where
- Equal
- Not Equal
- Greater Than
- Greater Than Equal
- Less Than
- Less Than Equal
- Like
- Not Like
- Contain
- Not Contain
- Start With
- End With
- In
- Not In
- Between
- Not Between
- Is Null
- Is Not Null
- Distinct
- Custom Filters
- Conditional Filters
- Manually Passing Filter Array (Livewire)
Describing the Problem
You have probably faced the situation where you needed to filter your query based on given parameters in url query-string and after developing the logics, You've had such a code:
This works, But it's not a good practice.
When the number of parameters starts to grow, The number of these kind of if
statements also grows and your code gets huge and hard to maintain.
Also it's against the Open/Closed principal of SOLID principles, Because when you have a new parameter, You need to get into your existing code and add a new logic (which may breaks the existing implementations).
So we have to design a way to make our filters logics separated from each other and apply them into the final query, which is the whole idea behind this package.
Usage
- First you need to install the package:
$ composer require faridlab/laravel-grammatical-query
-
Then you should
use
theFilterQueryString
trait in your model, And define$filters
property which can be consist of custom filters. - You need to use
filter()
method in your eloquent query. For example:
Available Methods
- Sort
- Comparisons
- In
- Like
- Where clause
Filters:
- fields:
array ― optional
- search:
string ― optional
- page:
integer default(1) ― optional
- limit:
integer default(25) ― optional
- relationship:
array ― optional
- count:
array ― optional
- withtrashed:
boolean default(false) ― optional
- orderby:
array ― optional
- fieldname[where]:
string|array ― optional
- fieldname[orwhere]:
string|array ― optional
- fieldname[eq]:
string|integer ― optional
- fieldname[notEq]:
string|integer ― optional
- fieldname[gt]:
string|integer ― optional
- fieldname[gtEq]:
string|integer ― optional
- fieldname[lt]:
string|integer ― optional
- fieldname[ltEq]:
string|integer ― optional
- fieldname[like]:
string ― optional
- fieldname[notlike]:
string ― optional
- fieldname[contain]:
string ― optional
- fieldname[notcontain]:
string ― optional
- fieldname[startwith]:
string ― optional
- fieldname[endwith]:
string ― optional
- fieldname[in]:
array ― optional
- fieldname[notin]:
array ― optional
- fieldname[between]:
array ― optional
- fieldname[notbetween]:
array ― optional
- fieldname[isnull]:
string ― optional
- fieldname[isnotnull]:
string ― optional
Query | Parameter | Note | SQL |
---|---|---|---|
AND | (where) | WHERE and | ...WHERE 1 = 1 AND fieldname = {search}... |
OR | (orwhere) | WHERE or | ...WHERE 1 = 1 OR fieldname = {search}... |
= | (eq) | EQual | ...WHERE fieldname = {search}... |
> | (gt) | Greater Than | ...WHERE fieldname > {search}... |
>= | (gtEq) | Greater Than EQual | ...WHERE fieldname >= {search}... |
< | (lt) | Less Than | ...WHERE fieldname < {search}... |
<= | (ltEq) | Less Than EQual | ...WHERE fieldname <= {search}... |
!= | (notEq) | NOT EQual | ...WHERE fieldname != {search}... |
LIKE | (like) | LIKE | ...WHERE fieldname LIKE {search}... |
LIKE %...% | (contain) | LIKE %...% | ...WHERE fieldname LIKE %{search}%... |
LIKE startwith | (startwith) | LIKE startwith% | ...WHERE fieldname LIKE {search}%... |
LIKE %endwith | (endwith) | LIKE %endwith | ...WHERE fieldname LIKE %{search}... |
NOT LIKE | (notlike) | NOT LIKE | ...WHERE fieldname NOT LIKE {search}... |
IN (...) | (in) | IN | ...WHERE fieldname IN({search})... |
NOT IN (...) | (notin) | NOT IN | ...WHERE fieldname NOT IN({search})... |
BETWEEN | (between) | BETWEEN | ...WHERE fieldname BETWEEN {search} AND {search}... |
NOT BETWEEN | (notbetween) | NOT BETWEEN | ...WHERE fieldname NOT BETWEEN {search} AND {search}... |
IS NULL | (isnull) | IS NULL | ...WHERE fieldname IS NULL... |
IS NOT NULL | (isnotnull) | IS NOT NULL | ...WHERE fieldname IS NOT NULL... |
DISTINCT | (distinct) | DISTINCT | ...WHERE DISTINCT(fieldname)... |
ORDER BY | (orderby) | ORDER BY | ...ORDER BY fieldname {orderby}... |
For the purpose of explaining each method, Imagine we have such data in our users
table:
id | name | username | age | created_at | |
---|---|---|---|---|---|
1 | mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
2 | reza | reza@startapp.id | reza123 | 20 | 2020-10-01 |
3 | hossein | hossein@startapp.id | hossein123 | 22 | 2020-11-01 |
4 | dariush | dariush@startapp.id | dariush123 | 22 | 2020-12-01 |
5 | farid | farid@startapp.id | faridlab | 21 | 2021-03-12 |
And assume our query is something like this:
Fields
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?fields[]=name&fields[]=email
Search
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?search=faridlab
id | name | username | age | created_at | |
---|---|---|---|---|---|
5 | farid | farid@startapp.id | faridlab | 21 | 2021-03-12 |
Page
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?page=1
Limit
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?limit=25
Relationship
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?relationship=role
https://startapp.id/api/v1/users?relationship[]=role&relationship[]=permissions
Count
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?count=addresses
https://startapp.id/api/v1/users?count[]=photos&count[]=accounts
Withtrashed
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?withtrashed=true
Orderby
Orderby is the equivalent to order by
sql statement which can be used flexible in FilterQueryString
:
Conventions:
In Users.php
Single sort
:
https://startapp.id/api/v1/users?orderby=created_at
Output:
name | username | age | created_at | |
---|---|---|---|---|
mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
reza | reza@startapp.id | reza123 | 20 | 2020-10-01 |
hossein | hossein@startapp.id | hossein123 | 22 | 2020-11-01 |
dariush | dariush@startapp.id | dariush123 | 22 | 2020-12-01 |
farid | farid@startapp.id | faridlab | 21 | 2021-03-12 |
- Note that when you're not passing parameter as array instead of string, it will be used as field name and order by 'asc' by default.
Multiple sort
s:
https://startapp.id/api/v1/users?orderby[name]=asc&orderby[email]=desc
Output:
name | username | age | created_at | |
---|---|---|---|---|
dariush | dariush@startapp.id | dariush123 | 22 | 2020-12-01 |
farid | farid@startapp.id | faridlab | 21 | 2021-03-12 |
hossein | hossein@startapp.id | hossein123 | 22 | 2020-11-01 |
mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
reza | reza@startapp.id | reza123 | 20 | 2020-10-01 |
Bare in mind that orderby
parameter with invalid values will be ignored from query and has no effect to the result.
Where
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username=faridlab
https://startapp.id/api/v1/users?username[]=faridlab
https://startapp.id/api/v1/users?username[where]=faridlab
Or where
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[where]=faridlab
https://startapp.id/api/v1/users?username[where][]=faridlab
https://startapp.id/api/v1/users?username[where][]=mehrad123
Equal
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[eq]=faridlab&email[eq][email protected]
Not Equal
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[notEq]=faridlab&email[notEq][email protected]
Greater Than
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?id[gt]=10
Greater Than Equal
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?id[gtEq]=10
Less Than
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?id[lt]=10
Less Than Equal
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?id[ltEq]=10
Like
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[like]=faridlab
Not Like
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[notlike]=faridlab
Contain
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[contain]=farid
Not Contain
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[notcontain]=farid
Start With
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[startwith]=farid
End With
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[endwith]=lab
In
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[in][]=faridlab&username[in][]=farid
Not In
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?username[notin][]=faridlab&username[notin][]=farid
Between
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?id[between][]=1&id[between][]=10
Not Between
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?id[notbetween][]=1&id[notbetween][]=10
Is Null
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?deleted_at[isnull]=null
https://startapp.id/api/v1/users?deleted_at[isnull]
Is Not Null
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?deleted_at[isnotnull]=null
https://startapp.id/api/v1/users?deleted_at[isnotnull]
Distinct
Convention:
In Users.php
Example:
https://startapp.id/api/v1/users?first_name[distinct]=true
https://startapp.id/api/v1/users?first_name[distinct]=1
https://startapp.id/api/v1/users?first_name[distinct]
Where Clause (default filter)
Generally when your query string parameters are not one of previous available methods, It'll get filtered by the default filter which is the where
sql statement. It's the proper filter when you need to directly filter one of your table's columns.
Conventions:
Assuming we want to filter name
, username
and age
database columns, In Users.php
Example:
https://startapp.id?name=mehrad
Output:
name | username | age | created_at | |
---|---|---|---|---|
mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
Example:
https://startapp.id?age=22&username=dariush123
Output:
name | username | age | created_at | |
---|---|---|---|---|
dariush | dariush@startapp.id | dariush123 | 22 | 2020-12-01 |
Example:
https://startapp.id?name[0]=mehrad&name[1]=dariush
Output:
name | username | age | created_at | |
---|---|---|---|---|
mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
dariush | dariush@startapp.id | dariush123 | 22 | 2020-12-01 |
Example:
https://startapp.id?name[0]=mehrad&name[1]=dariush&username[0]=mehrad123&username[1]=reza1234
Output:
name | username | age | created_at | |
---|---|---|---|---|
mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
Bare in mind that default
filter parameter with invalid values will be ignored from query and has no effect to the result.
Custom Filters
By custom filters you can define your own methods as filters. This helps with the Open/Closed of SOLID principles, Hence each time a new filter is needed, you don't have to edit previous filters and you can just write a separate method for it.
Let's create a custom filter. Assuming you want to create a filter named all_except
which retrieves all users except the one that is specified:
In Users.php
To test our newly added filter:
https://startapp.id?all_except=mehrad
Output:
name | username | age | created_at | |
---|---|---|---|---|
reza | reza@startapp.id | reza123 | 20 | 2020-10-01 |
hossein | hossein@startapp.id | hossein123 | 22 | 2020-11-01 |
dariush | dariush@startapp.id | dariush123 | 22 | 2020-12-01 |
Note that your custom defined filters have the most priority which means you can even override available filters.
For example lets change in
filter in a way that only accepts 3 values:
In Users.php
Another good example for custom filters are when you don't want to expose your database table's column name. For example assume we don't want to expose that we have a column named username
in users
table:
In Users.php
https://startapp.id?by=dariush123
Output:
name | username | age | created_at | |
---|---|---|---|---|
dariush | dariush@startapp.id | dariush123 | 22 | 2020-12-01 |
Minor Tip
In order to prevent your model to get messy or populated with filter methods, You can create a trait for it and put everything about filters inside the trait.
Conditional Filters
The $filters
property in your model is acting kind of global for that model. It means when you use filter()
method on your eloquent query, it'll always performs all the $filters
filters.
There might be situations that based on a condition you need to specify which filters exactly you wish to be filtered.
To achieve this you can specify your desired filters as arguments in filter()
method.
Example:
In your query:
in=name,mehrad,reza&like=name,mehrad
Output:
name | username | age | created_at | |
---|---|---|---|---|
mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
reza | reza@startapp.id | reza123 | 20 | 2020-10-01 |
If the in
argument was not specified, The result of query would be only one record (mehrad
).
Another example:
In your query:
like=name,mehrad,reza,dariush,hossein&name[0]=mehrad&name[1]=hossein&username=mehrad
Output:
name | username | age | created_at | |
---|---|---|---|---|
mehrad | mehrad@startapp.id | mehrad123 | 20 | 2020-09-01 |
hossein | hossein@startapp.id | hossein123 | 22 | 2020-11-01 |
Manually Passing Filter Array (Livewire)
When using Livewire to filter data, subsequent query string changes do not trigger new requests. We can work around this by manually passing an array of filters.
Example:
Another example:
You can also combine this with conditional filters:
The above would only query the username (not the email) since only the username was included as a conditional.
Note that the filter array must be passed before the conditionals.