Download the PHP package thejawker/laravel-interrogator without Composer
On this page you can find all versions of the php package thejawker/laravel-interrogator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download thejawker/laravel-interrogator
More information about thejawker/laravel-interrogator
Files in thejawker/laravel-interrogator
Package laravel-interrogator
Short Description Interrogates the request and applies JSON API rules to the Laravel Query Builder.
License MIT
Homepage https://github.com/thejawker/laravel-interrogator
Informations about the package laravel-interrogator
Just Ask, And We'll Get It For You
Installation
Require the package from Composer:
Usage
You can interrogate()
any Laravel Model in a Controller. Basic setup is easy peasy but does not provide proper security.
Security can be added where needed.
Filtering
You can use filters to add where clauses to your query builder. A filter is depicted by the filter[column]=value
variable.
Wildcard Filter
You can use wildcard operators in string filters. It is possible to create fairly complex filters this way.
List Filter
A comma-seperated list can be used as a filter. This will in turn run a whereIn
on the QueryBuilder
.
Note that you can currently not combine this with the wildcard filter as follows:
jpg,pn*
.
And or Or
You can specify the select's and
or or
conditions.
For example if you want to fetch Users
with a gmail email address AND and a Dutch mobile phone numbers, you could do the following.
By default the or
operator is used, however you are free to explicitly
Math Operations
It is possible to use a variety of different math operators in a filter.
Operator | Math Operator | Name | Example |
---|---|---|---|
ge | >= | Greater or equal | filter[price]=[ge]500 |
gt | > | Greater than | filter[price]=[gt]500 |
le | <= | Less or equal | filter[price]=[le]500 |
lt | < | Less than | filter[price]=[lt]500 |
Of course you can combine this with the And or Or operator.
Default filters
It is possible to conveniently set default filters on the Interrogator. These can be overriden by the api consumer (client). Under the hood it will set the filter to the request.
If you want you can filter the QueryBuilder beforehand as well.
Null Filter
Sometimes you need to make sure a column is null. You can for example do the following:
Security
For some models you might want to prevent filtering on certain columns. The way you do this is by allowing a selection of fields. A not allowed filter will throw an error.
NOTE: nested filters are currently not supported.
Sorting
Sorting works according to the JSON spec by using the sort
parameter on the request with the column name as a value.
You can sort by asc:
You can also sort by descending by adding a hyphen -
:
Security
Like filtering there's some security baked in to the package. You can allow certain sorting columns by adding ->allowSortBy(['email'])
onto the Interrogator.
Default sorting
Also default sorting can be defined on the Interrogator
. Like the filtering; this is added on the request.
Chain shortcuts
You can keep chaining on with the Interrogator. Pagination
and get
are made shortcuts allowing you to do the following.
Your Life Made Easy
The interrogate()
helper function tries to make your life easier by allowing various types.
We try to solve your intent when you enter one of the following types.
Alternatives
For everything is a really nice Spatie package. I needed to be able to do some math specific operations that were not supported in the mentioned package.
Test
License
The MIT License (MIT). Please see License File for more information.