Package Data | |
---|---|
Maintainer Username: | wwtg99 |
Maintainer Contact: | wwtg99@gmail.com (wwtg99) |
Package Create Date: | 2017-03-20 |
Package Last Update: | 2017-06-20 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-30 15:24:20 |
Package Statistics | |
---|---|
Total Downloads: | 89 |
Monthly Downloads: | 1 |
Daily Downloads: | 0 |
Total Stars: | 1 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
Add helper traits for restful query.
composer require wwtg99/restful-helper
This trait parse url queries for eloquent model.
Suppose you have a resource controller in route /users
and connect to User model.
Add filterableFields in your model.
$filterableFields = ['role'];
Then use /users?role=admin
to get user with role admin.
Supported operators:
Use /users?sort=-role,created_at
to sort by role desc and created_at asc, comma(,) to separate.
Use /users?fields=name,role,created_at
to show only name, role and created_at.
Also can config $selectableFields
to restrict selectable fields.
$selectableFields = ['name', 'role'];
Then only name and role can be selected to show.
Use fields=count
to count results /users?fields=count
.
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.
use RestHelperTrait;
$user = User::index()->get();
// Or, use custom inputs array
$user = User::index(request()->all())->get();
$user = User::index()->where('name', 'admin')->get();
use RestfulControllerTrait;
protected function getModel()
{
return User::query();
}
Model should use RestHelperTrait to have index function.
Route::resource('/path', 'controller');
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.
Add new route for batch process. Batch process read content body as json object.
{"GET": [1,2,3]}
Use key GET
and id array as value. Return data or error in the same sequence as query.
{"CREATE": [{"name":"aaa"}, {"name":"bbb"}]}
Use key CREATE
and object array as value. Return data or error in the same sequence as query.
{"UPDATE": {"1": {"name":"ccc"}, "2": {"name": "ddd"}}}
Use key UPDATE
, id and data pairs as value. Return data or error in the same sequence as query.
{"DELETE": [1,2,3]}
Use key DELETE
and id array as value. Return {"code": 204} or error in the same sequence as query.
RestfulControllerTrait
in your Controllerclass TestController extends Controller
{
use RestfulControllerTrait;
protected $creatableFields = ['name', 'email', 'password'];
protected $updateableFields = ['email'];
protected function getModel()
{
return User::query();
}
}
Route::match(['GET', 'POST'], 'test/batch', 'TestController@batch');
Return { "GET": [{"id":1, "name":"a", "email": "a@a.com"}, {"id":1, "name":"a", "email": "a@a.com"}], "CREATE": [{"id": 3, "name":"aaa", "email":"aa@a.com"}], "UPDATE": {"1": ["id":1, "name": "bb", "email": "a@a.com"], "2": ["id":2, "name":"a", "email":"b@b.com"], "DELETE": [{"code":204},{"code":204}] }