Download the PHP package plokko/querable-resource without Composer
On this page you can find all versions of the php package plokko/querable-resource. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package querable-resource
QuerableResource
A Laravel class that helps querying(paging and filtering) and returning Eloquent resources from Ajax.
Supports Laravel 5.5 Resources.
Installation
Install it throught composer composer require plokko/querable-resource
Quick start
First of all you need to extend the Plokko\QuerableResource\QuerableResource
class implementing the getQuery method that returns the base query:
Next we're going to instantiate it and return it to the request
The page should return a basic resource with all the results
Pagination
To enable pagination edit the $paginate protected propriety or via the pagination() method setting it to the wanted page size or to null to disable it.
The propriety $paginations specify the user-selectable available values, if the value is null
only $paginate user values will be discarted in favor of $paginate, if an int value is specified it will be set as the maxium value (in this case the user could select pagination up to 100 with default of 30 items per page); if an array (of integers) is specified the user value must be contained in the array or $paginate value will be used (ex. [10,20,30,50]
)
Filtering results
To filter results extend the protected propriety $filteredFields specifying what fields you want to filter
You do not need to pass anything to the resource, the filtering is automatic and transparent.
By default the comparison is done with the LIKE 'value%'; for example the page /test?name=a
will search all the user with a name starting with 'a'.
Alias
If you want to use a query parameter different from the field you can specify the alias as key for the field:
Advanced filtering options
Instead of the field name you can specify an array of filtering options like
- field - string (required) - corresponding field name, required
- type - string - type of comparaison (ex. '=','like','>','>=',...)
Changing query field name
If you want to group all filtering field in an input group, for example filter[filed_name], you need to edit the protected propriety $filterQueryParameter
The url of the last example will now be /test?filter[name]=a
Define filtering rules
Custom filtering function
If your query needs a more fine-tuning you can override the default filter function and specify your custom filtering function:
Don't worry, pagination and query field name will also be automatically applied to your filtering, no need to implement them!
Returning custom resource
If you want to filter the results with a custom resource you can do it by simply specifying the name of your Resource class in the protected field $useResource:
The class will now return the specified resource (as a collection); for further detail on resources see Laravel 5.5 API resources doc.
Javascript integration
This plugin contains some Javascript assets to help integrate with the php counterpart.
This plugin includes two main helper:
- ResourceQuery For building an Ajax request to the php plugin counterpart
- QuerableResult Wrapper of ResourceQuery that returns a querable result instead of a query, usefull especially in Vue.js applications