Download the PHP package meiko/laravel-filterable without Composer
On this page you can find all versions of the php package meiko/laravel-filterable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download meiko/laravel-filterable
More information about meiko/laravel-filterable
Files in meiko/laravel-filterable
Package laravel-filterable
Short Description Provides JSON API filter support to models
License MIT
Informations about the package laravel-filterable
laravel-filterable
Use url parameters to easily sort, filter and search eloquent models.
Table of Contents
Click to expand
- [laravel-filterable](#laravel-filterable) - [Table of Contents](#table-of-contents) - [Getting started](#getting-started) - [Installation](#installation) - [Configuration](#configuration) - [Usage](#usage) - [Sorting](#sorting) - [Ascending order](#ascending-order) - [Descending order](#descending-order) - [Filtering](#filtering) - [Id columns](#id-columns) - [Custom filters](#custom-filters) - [Searching](#searching) - [Prerequisites](#prerequisites) - [Usage](#usage-1)
Getting started
Installation
Install with composer
Configuration
Create new configuration file filterable.php
in your config directory (config/filterable.php
).
Usage
To enable usage for your model, you need to apply the provided Filterable trait to your model.
Next, call the filters
in your controllers.
Example:
Sorting
To sort your eloquent models, use the sort
url param:
sort=<column-name>
.
Ascending order
Example: Sort post's in ascending order by the posts updated_at
column.
Descending order
To sort the results in descending order, prefix the url param value with -
.
Filtering
To filter your eloquent models, you need to provide the column name
, comparison type
and value
in the url.
<column-name>=<comparison-type>=<value>
Examples:
Fetch posts where the title
column has text like Lorem ipsum
Fetch posts where the title
is NOT like Lorem ipsum
Fetch posts where the read_count
column has value bigger than 100.
Fetch posts where the title
column contains one of: Lorem ipsum
, Hello
or First
NOTE: When providing multiple values, separate the elements with |
character. Only the bw
, not-bw
, in
, not-in
comparison types support multiple values.
See Field.php for all the supported comparison types.
Id columns
If you need to filter models by one their relations and you use some kind of id obfuscation library (like hashids) then queries with obfuscated id's won't work.
To fix this, you can set custom idResolver
(in config/filterable.php
) to "decode" your id values before they are used in queries.
config/filterable.php
app/Models/IdResolver.php
Custom filters
If the provided comparison types are not enough or you wish to have some additional logic for the filtering process you can also create your own filters.
Example: Users have one-to-many relation with posts. We need to filter posts by their creators username.
Now we can use the username
url param in our query.
Searching
Prerequisites
To use the search, you first need configure the columns which can be searched.
Usage
To search, use the _q
url parameter.
The query tries to find matches for searchword
in the Post's title
and content
columns.