Download the PHP package bayareawebpro/searchable-resource without Composer
On this page you can find all versions of the php package bayareawebpro/searchable-resource. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bayareawebpro/searchable-resource
More information about bayareawebpro/searchable-resource
Files in bayareawebpro/searchable-resource
Package searchable-resource
Short Description Searchable Resource Builder for Laravel
License MIT
Homepage https://github.com/bayareawebpro/searchable-resource
Informations about the package searchable-resource
Laravel Searchable Resource Builder
Searchable Resource Builder is an abstraction for building searchable resource responses in Laravel applications. Extract query logic into reusable chunks while using a fluent builder interface for dealing with searchable / filterable / sortable requests and JSON / API Resources.
Basic Usage
SearchableResources implement the Responsable
interface which allows them to be
returned from controllers easily. It can also be used with blade.
The method accepts instances of Eloquent Builder.
Ordering and Sorting
You can specify as many orderable columns as you wish.
The default settings:
- order_by ID
- sort DESC
Full Example
Blade / View Example
Execute the query and return a view with the items and options.
JSON Resources
SearchableResources are generic JsonResources by default. You can easily specify which resource class should be used to map your models when building the response.
Must extend
JsonResource
.
Invokable Queries
Queries are expressed as invokable classes that extend the AbstractQuery
class
which contains logic per request field. Queries can apply to multiple attributes/columns
as well as multiple inputs for orWhere
clauses.
php artisan make:searchable NameQuery
The following is an example of a generic name query:
ConditionalQuery Contract
Queries that implement the ConditionalQuery
Contract will only be applied when
their applies
method returns true
.
By default an query that extends AbstractQuery
class using the ConditionalQuery
contract
already implements this method for you by calling the filled
method on the request.
Override the parent method to customize.
Validation
Queries can specify their own validation rules by implementing the ValidatableQuery
contract to be merged the rules for the searchable collection.
ValidatableQuery Contract
Queries that implement the ValidatableQuery
Contract will have their returned rules
merged into the validator, otherwise the rules will be ignored.
ProvidesOptions Contract
Queries can provide options that will be appended to the request options
data by implementing the ProvidesOptions
contract. This method should return
a flat array of values that are injected into the response query options data.
Options Formatting
Options can be formatted with labels for usage with forms and filters by calling
the labeled()
method on the builder. The labeled method accept a boolean
value which can be used to enable when the request has a session.
You can return preformatted options (label / value array) from queries or use the formatter to generate labeled options.
API Options Schema
Options for API requests are typically not-formatted for speed.
Blade Options Schema
Options for Blade requests can be formatted for usability.
FormatsOptions Contract
You can override the default formatter by specifying a formatter instance.
Setting Up Default Options
You can setup a resolving callback in a service provider to pre-bind options to every instance.
Adding Queries:
Queries can be added two ways. First by referencing the class string for easy bulk usage.
Second by instantiating each query using the make
method. This can be useful when you need
more methods and logic to determine usage.
Appendable Data
Attributes and fields can be appended to the response by using the following methods:
For model attributes:
For additional data (appended to the response):
For request fields (appended to the query in response):
When Condition Callback
You can use a callback or invokable class for more control with less method chaining.
Tap Callback
Useful for configuring the builder via an invokable class.
Response Output
The relevant query parameters and request options are appended to the output for
convenience. Two additional properties have been added to the pagination parameters
to remove the need for conditionals on the client / user side isFirstPage
and isLastPage
making pagination buttons easy to disable via props (Vue | React).
Note: If the
pagination
method is not used, all pagination related properties will be filtered from the output data.