Download the PHP package illuminatech/data-provider without Composer
On this page you can find all versions of the php package illuminatech/data-provider. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package data-provider
Laravel Data Provider
This extension allows building of the complex search queries based on the request in Laravel. In particular, it is useful for REST API composition.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the "require" section of your composer.json.
Usage
This extension allows building of the complex search queries based on the request data. It handles filtering, sorting, pagination, include of extra fields or relations on demand. Both Eloquent Active Record and plain database queries are supported.
This extension provides Illuminatech\DataProvider\DataProvider
class, which wraps given data source object like database
query builder, and provide the means to define controller-level interaction to search through this data source.
Usage example:
This example will respond to the following request:
Same example with the plain database query usage:
Data provider defines only a few methods for the data querying:
get()
- returns all records matching requestpaginate()
- returns all records matching request with a paginatorsimplePaginate()
- returns all records matching request with a simple paginatorcursorPaginate()
- returns all records matching request with a cursor paginator
However, you can use prepare()
method to get data source object, adjusted to given request, to invoke the method you need.
For example:
Method prepare()
is immutable, leaving original data source object intact. Thus, you can re-use same data provider to
process several different search requests. For example:
Specifying Data Source
There are several ways to specify a data source for the DataProvider
:
- instance of
Illuminate\Database\Eloquent\Builder
- instance of
Illuminate\Database\Query\Builder
- instance of Eloquent relation like
Illuminate\Database\Eloquent\Relations\HasMany
- name of the Eloquent model class
For example:
Note: this extension does not put explicit restriction on the data source object type - it simply expected to match database query builder notation. Thus, you may create a custom query builder class, which works with special data storage like MongoDB or Redis, and pass its instance as a data source. If its methods signature matches
\Illuminate\Database\Query\Builder
- it should work. Although it is not guaranteed.
Configuration
You can publish predefined configuration file using following console command:
This will create an application-wide configuration for all DataProvider
instances. You can see its example at config/data_provider.php.
You may adjust configuration per each DataProvider
instance, using second argument of its constructor.
For example:
Additional configuration will be merged recursively with one you specified at your "config/data_provider.php" file, so you should specify only those keys you wish to change.
Filtering
Filters setup example:
This example will respond to the following request:
Tip: you may disable filters grouping in request by setting
null
asdata_provider.filter.keyword
configuration value.
While specifying filter attribute, you can use a dot ('.') notation to make filter challenge against relation instead of main source.
In this case Illuminate\Database\Eloquent\Concerns::QueriesRelationships::whereHas()
will be executed under the hood.
However, this behavior will apply only for Eloquent query and relations.
For example:
List of supported filters:
- Illuminatech\DataProvider\Filters\FilterExact
- Illuminatech\DataProvider\Filters\FilterSearch
- Illuminatech\DataProvider\Filters\FilterCallback
- Illuminatech\DataProvider\Filters\FilterCompare
- Illuminatech\DataProvider\Filters\FilterIn
- Illuminatech\DataProvider\Filters\FilterLike
- Illuminatech\DataProvider\Filters\FilterScope
- Illuminatech\DataProvider\Filters\FilterTrashed
Please refer to the particular filter class for more details and examples.
You can create your custom filter implementing Illuminatech\DataProvider\FilterContract interface.
Sorting
Sorting setup example:
You may enable multisort support for the data provider setting data_provider.sort.enable_multisort
configuration value to true
.
For example:
Note: sort parameter for multi-sort can be passed both as comma separated string and as an array.
Pagination
Data provider defines following pagination methods, wrapping the ones provided by query builder:
paginate()
- returns all records matching request with a paginatorsimplePaginate()
- returns all records matching request with a simple paginatorcursorPaginate()
- returns all records matching request with a cursor paginator
In addition to Laravel standard pagination behavior, these methods also allow control over page size via request parameters. For example:
You may control page size boundaries per each data provider using constructor configuration parameter. For example:
If data_provider.pagination.appends
enabled, all pagination methods will automatically append passed request data to
the created paginator instance, so you do not need to invoke Illuminate\Contracts\Pagination\Paginator::appends()
manually.
Include Relations
While creating an API, you may allow its client to "expand" particular entities, including their relations to the HTTP response.
Available for inclusion relations setup example:
Selecting Fields
While creating an API, you may allow its client to specify the list of fields to be returned by particular listing endpoint. This may be useful to reduce HTTP traffic, skipping large text fields in response.
Selectable fields setup example:
You may specify selectable fields for the related models as well. For example:
Note that passing fields for the particular relation causes its eager loading. This way you actually declare "includes"
while writing "fields". This may create an inconsistency in your API, as it allows loading of the particular relation via
"fields", but does not allow its loading via "includes". It is your responsibility to setup includes()
and fields()
in consistent way.
JSON API Specification Support
This extension is compatible with JSON API Specification. However, the default configuration for the pagination mismatches it, since it provides compatibility with native Laravel pagination. But you can easily fix this with a proper configuration. For example:
Dedicated Data Providers
You may create a custom data provider class dedicated to the specific use case. Such approach allows to organize the code and keep your controllers clean.
This goal can be easily achieved using Illuminatech\DataProvider\DedicatedDataProvider
as a base class. It predefines
a set of methods named define*
, like defineConfig()
, defineFilters()
and so on, which you can override, creating a
structured custom class. Also note, that unlike other methods, __construct()
is exempt from the usual signature compatibility
rules when being extended. Thus, you can specify its signature in your class as you like, defining your own dependencies.
For example: