Download the PHP package hds-solutions/laravel-api-helpers without Composer
On this page you can find all versions of the php package hds-solutions/laravel-api-helpers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hds-solutions/laravel-api-helpers
More information about hds-solutions/laravel-api-helpers
Files in hds-solutions/laravel-api-helpers
Package laravel-api-helpers
Short Description A package with helpers to generate APIs on your Laravel project
License GPL-3.0
Informations about the package laravel-api-helpers
Laravel API Helpers
This library simplifies the process of building API controllers by providing convenient classes for managing filtering, ordering, relationship loading, and pagination of resource collections.
Features
- Easy management of request query filters for filtering resource collections based on allowed columns.
- Simplified sorting of resource collections based on allowed columns.
- Convenient loading of extra relationships for resource collections.
- Pagination support for resource collections.
Installation
Dependencies
- PHP >= 8.0
- Laravel Framework >= 9.0
Via composer
Usage
To make use of the library, you will need to create specific classes that extend the provided abstract classes. The provided classes contain the implementation of the necessary logic for each feature (filtering, sorting, relationship loading, and pagination).
ResourceFilters
The ResourceFilters
class manages the query filters for resource collections.
It allows you to define the allowed columns and their corresponding filter operators.
In the extended class, you can define the list of allowed columns that can be used for filtering, along with their allowed operators.
The available operators are:
eq
: Translates to afield_name = "value"
filter.ne
: Translates to afield_name != "value"
filter.has
: Translates to afield_name LIKE "%value%"
filter.lt
: Translates to afield_name < "value"
filter.lte
: Translates to afield_name <= "value"
filter.gt
: Translates to afield_name > "value"
filter.gte
: Translates to afield_name >= "value"
filter.in
: Translates to afield_name IN ("value1", "value2", ...)
filter.btw
: Translates to afield_name BETWEEN "value1" AND "value2"
filter.
Operators are also grouped by field type:
string
: Translates to the operatorseq
,ne
andhas
.numeric
: Translates to the operatorseq
,ne
,lt
,lte
,gt
,gte
,in
, andbtw
.boolean
: Translates to the operatorseq
andne
.date
: Translates to the operatorseq
,ne
,lt
,lte
,gt
,gte
, andbtw
.
Example implementation
You just need to extend the ResourceFilters
class and define the allowed filtrable columns.
You can also override the default filtering implementation of a column by defining a method with the same name as the filtrable column. The method must have the following arguments:
Illuminate\Database\Eloquent\Builder
: The current instance of the query builder.string
: The operator requested for filtering.mixed
: The value of the filter.
Example requests
-
Filtering by country name:
Example response:
-
Filtering by country size:
Example response:
-
Filtering by countries that have more than N regions:
Example response:
ResourceOrders
The ResourceOrders
class manages the sorting of resource collections.
It allows you to define the allowed columns to sort the resource collection and a default sorting fields.
In the extended class, you can define the list of allowed columns that can be used for sorting the resource collection.
Example implementation
You just need to extend the ResourceOrders
class and define the allowed sortable columns.
You can also override the default sorting implementation of a column by defining a method with the studly version of the sortable column. The method must have the following arguments:
Illuminate\Database\Eloquent\Builder
: The current instance of the query builder.string
: The direction of the sort.
Example requests
The request sorting parameters must follow the following syntax: order[{index}][{direction}]={field}
-
Sorting by country name:
Example response:
-
Sorting by country name and regions count in descending order:
Example response:
ResourceRelations
The ResourceRelations
class manages the loading of extra relationships for resource collections.
It allows you to specify the allowed relationships to be loaded and the relationships that should always be loaded.
In the extended class, you can define the list of allowed relationships that can be added to the resource collection.
Example implementation
You can also capture the loaded relationship to add filters, sorting, or any action that you need. The method must have the following arguments:
Illuminate\Database\Eloquent\Relations\Relation
: The instance of the relationship being loaded.
Example requests
-
Loading countries with their regions relationship collection:
Example response:
PaginateResults
The PaginateResults
class handles the pagination of resource collections.
It provides support for paginating the results or retrieving all records.
Example requests
-
Request all countries:
Example response:
Controller implementation
Here is an example of a controller using the Pipeline
facade to implement all the previous features.
More request examples
Example response:
Example response:
Example response:
Extras
Before and after callbacks
The ResourceFilters
and ResourceOrders
classes have two methods (before
& after
) that allow a better customization on the query builder. You can override them to make more manipulations to the query builder.
ResourceRequest
The ResourceRequest
class has the following features:
- The
hash()
method gives you a unique identifier based on the query parameters. - The
authorize()
method is a WIP feature that will handle resource access authorization.
Caching requests
You can use the hash()
method of the ResourceRequest
class and use it as a cache key. The parameter cache
is ignored and not used to build the request identifier.
In the following example, we capture the cache
request parameter to force the cache to be cleared.
Security Vulnerabilities
If you encounter any security-related issues, please feel free to raise a ticket on the issue tracker.
Contributing
Contributions are welcome! If you find any issues or would like to add new features or improvements, please feel free to submit a pull request.
Contributors
Licence
This library is open-source software licensed under the MIT License. Please see the License File for more information.