Download the PHP package wwtg99/restful-helper without Composer
On this page you can find all versions of the php package wwtg99/restful-helper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package restful-helper
Restful query parser trait for Laravel eloquent model
Add helper traits for restful query.
Installation
Parse query
This trait parse url queries for eloquent model.
Suppose you have a resource controller in route /users
and connect to User model.
filters
Add filterableFields in your model.
Then use /users?role=admin
to get user with role admin.
Supported operators:
- equal: /users?id=1
- equal or greater than: /users?id>=1
- equal or less than: /users?id<=2
- not equal: /users?id!=1
- like: /users?id*=1
sorts
Use /users?sort=-role,created_at
to sort by role desc and created_at asc, comma(,) to separate.
select fields
Use /users?fields=name,role,created_at
to show only name, role and created_at.
Also can config $selectableFields
to restrict selectable fields.
Then only name and role can be selected to show.
Use fields=count
to count results /users?fields=count
.
pagination
Use /users?limit=10&offset=10
to limit 10 and offset 10 records, offset can be omitted, default 0.
Also can use page
and page_size
, /users?page=2&page_size=15
, page_size can be omitted, default 15.
Usage
Eloquent model trait
-
Add RestHelper trait in eloquent model.
-
Use index method.
- Also combine with other methods.
Controller trait
-
Add RestfulController trait in resource controller.
- Implement getModel method.
Model should use RestHelperTrait to have index function.
-
Add controller to routes
- Each resource method (index, show, store, update, destroy) has three parts.
- parse requests
- handle action
- response
Override these template functions to change the default behaviors.
Add $creatableFields = ['field1', 'field2'] to restrict fields to store. Add $updateableFields = ['field1', 'field2'] to restrict fields to update.
Batch Process
Add new route for batch process. Batch process read content body as json object.
- Get resources:
Use key GET
and id array as value. Return data or error in the same sequence as query.
- Create resources:
Use key CREATE
and object array as value. Return data or error in the same sequence as query.
- Update resources:
Use key UPDATE
, id and data pairs as value. Return data or error in the same sequence as query.
- Delete resources:
Use key DELETE
and id array as value. Return {"code": 204} or error in the same sequence as query.
Batch Usage
-
Add
RestfulControllerTrait
in your Controller -
Add route
- Send batch query POST /test/batch {"GET":[1,2],"CREATE":[{"name":"aaa","email":"[email protected]"}],"UPDATE":{"1":{"name":"bb"},"2":{"email":"[email protected]"}},"DELETE":[5,6]}
Return { "GET": [{"id":1, "name":"a", "email": "[email protected]"}, {"id":1, "name":"a", "email": "[email protected]"}], "CREATE": [{"id": 3, "name":"aaa", "email":"[email protected]"}], "UPDATE": {"1": ["id":1, "name": "bb", "email": "[email protected]"], "2": ["id":2, "name":"a", "email":"[email protected]"], "DELETE": [{"code":204},{"code":204}] }