Download the PHP package asemalalami/laravel-advanced-filter without Composer
On this page you can find all versions of the php package asemalalami/laravel-advanced-filter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download asemalalami/laravel-advanced-filter
More information about asemalalami/laravel-advanced-filter
Files in asemalalami/laravel-advanced-filter
Package laravel-advanced-filter
Short Description Laravel Advanced Filter
License MIT
Informations about the package laravel-advanced-filter
Laravel Advanced Filter
This package allows you to filter on laravel models
You can choose fields to filtering and customize its data-types, aliases and excepted operators, you can add/customize your request format, and you add new operators or overwrite the existed operators
Installation
You can install the package via composer:
The package will automatically register its service provider.
You can optionally publish the config file with:
These default config file that will be published: Config File
Usage
- use
HasFilter
trait in the model - add fields in the implementation of the abstract function
setupFilter
- call the
filter
scope in your controller
Query Format
Query format is the shape that you want to send your query(filters) in the request. the package support 3 formats, and you can create a new format.
-
json
(default): the filters will send as json in the request -
array
: the filters will send as array in the request -
separator
: the filters will send as well as in thearray
format, but separated by a separator(^
default)the format sets with a separator symbol
separator:^
set the default query format in the config file
query_format
attribute
Create a new query format:
- create a new class and extends it from
QueryFormat
:class MyFormat extends QueryFormat
- implement the abstract function
format
that returnsFilterRequest
object - add the class to the config file in
custom_query_format
attribute:'custom_query_format' => MyFormat::class,
Fields
Normal Field options:
- field name is the column name
- alias is the key that you want to send in the request
- data-type: by default it set from model
casts
, if you want to set custom data-type, usesetDatatype
- operators: the field will accept all operators unless you use
setExceptedOperators
to exclude some operators -
a relational field: only set the field name by
.
separatorchannel.name
,channel.type.name
you can define field name by
.
separator, but you want to consider it as a non relational field by passfalse
forinRelation
parameter (used in NoSQL DB or join between tables) -
customize a field query, you can make a scope for the field to customize the filter behavior. scope name must be combined 3 sections :
- scope
- the value of
prefix_scope_function
key in config file (where
is the default) - field name(or relation name) for example
email
you can customize a relational field by define the scope in the relation model OR define scope by relation name
you can use
applyOperator
function to use the default behavior$builder->applyOperator($operator, $field, $value, $conjunction);
You can add fields to a model by using 4 functions:
-
addField
(string $field, string $alias = null, ?bool $inRelation = null): by default alias value same as field name value -
addFields
($fields): accept an array of field and aliases: -
addCountField
(string $relation, string $alias = null, callable $callback = null): add a field from count of relation, use can customize the count query and alias(by default is concat relation name(snake case) and_count
) addCustomField
(string $alias, string $sqlRaw, $relation = null): add a field from raw sql query
General Search
You can enable general search on some fields, and you can specify the operator (startsWith
is the default operator)
Conjunction
Currently, the package support one conjunction between all fields
and
| or
, default conjunction attribute in the config file default_conjunction
Operators
The package has many operators, you can create new operators, and you can customize the operators aliases that you want to send in the request
- Equals (
=
,equals
) - NotEquals (
!=
,notEquals
) - GreaterThan (
>
,greater
) - GreaterThanOrEqual (
>=
,greaterOrEqual
) - LessThan (
<
,less
) - LessThanOrEqual (
<=
,lessOrEqual
) - In (
|
,in
) - NotIn (
!|
,notIn
) - Contains (
*
,contains
) - NotContains (
!*
,notContains
) - StartsWith (
^
,startsWith
) - NotStartsWith (
!^
,notStartsWith
) - EndsWith (
$
,endsWith
) - NotEndsWith (
!$
,notEndsWith
) - Between (
><
,between
)
Create a new Operator:
- create a new class and extends it from
Operator
:class MyOperator extends Operator
- implement the abstract function
apply
andgetSqlOperator
(used as a default sql operator for count and custom field) - add the class in the config file in
custom_operators
attribute:'custom_operators' => [MyOperator::class => ['my-op', '*']],
Data Types:
- boolean
- date
- datetime
- numeric
- string
Config
All versions of laravel-advanced-filter with dependencies
illuminate/database Version ^5.8|^6.0|^7.0|^8.0
illuminate/support Version ^5.8|^6.0|^7.0|^8.0