Basic CRUD API Methods that can be extended for your models by default has a list, show, update, add and delete endpoint to interact with your model.
Install via composer
composer require phpsa/laravel-api-controller
php artisan vendor:publish --provider="Phpsa\LaravelApiController\ServiceProvider" --tag="config"
Generate a new Api Controller, Repository and Route via php artisan make:api {ModelName}
This will create a Api/ModelNameControlelr for you and you will have the basic routes in place as follows:
- GET
api/v1/{model_name}
- list all/paged/filtered (index) - GET
api/v1/{model_name}/$id
- Show a specified id (show) - POST
api/v1/{model_name}
- Insert a new record (store) - PUT
api/v1/{model_name}/$id
- Update an existing record (update) - DELETE
api/v1/{model_name}/$id
- Delete an existing record (destroy)
You can override the methods by simply putting in your own methods to override - method names in braces above
For the get command you can filter by using the following url patterns
Seperator | Description | Example | Result |
---|---|---|---|
= |
Equals | ?filter[field]=hello | select ... where field = 'hello' |
!= |
Not Equals | ?filter[field!]=hello | select ... where field != 'hello' |
<> |
Not Equals (alt) | ?filter[field<>]=hello | select ... where field != 'hello' |
> |
Greater Than | ?filter[field>]=5 | select ... where field > 5 |
>= |
Greater Or Equal to | ?filter[field=>]=5 | select ... where field >= 5 |
< |
Less Than | ?filter[field<]=5 | select ... where field <> 5 |
<= |
Less Or Equal to | ?filter[field=<]=5 | select ... where field <= 5 |
~ |
Contains (LIKE with wildcard on both sides) | ?filter[field~]=hello | select ... where field like '%hello%' |
^ |
Starts with (LIKE with wildcard on end) | ?filter[field^]=hello | select ... where field like 'hello%' |
$ |
Ends with (LIKE with wildcard on start) | ?filter[field$]=hello | select ... where field like 'hello%' |
!~ |
Not Contains (LIKE with wildcard on both sides) | ?filter[field!~]=hello | select ... where field not like '%hello%' |
!^ |
Not Starts with (LIKE with wildcard on end) | ?filter[field!^]=hello | select ... where field not like 'hello%' |
!$ |
Not Ends with (LIKE with wildcard on start) | ?filter[field!$]=hello | select ... where field not like 'hello%' |
By default all fields are returned, you can limit that to specific fields in the following ways:
- Api Controller parameter
$defaultFields
default asprotected $defaultFields = ['*'];
- switch to include an array of fields - fields param in url querystring: ie
fields=id,name,age
= will only return those, this will also override the above.
- Using the relationships defined in your models, you can pass a comma delimited list eg
with=join1,join2
which will return those joins (one or many)
- Sorts can be passed as comma list aswell, ie
sort=age asc
orsort=age asc,name desc,eyes
- generates sql ofsort age asc
andsort age asc, name desc, eyes asc
respectively - Default sort can also be added on the controller using by overrideing the
protected $defaultSort = null;
parameter
- pagination can be enabled/disbled on the controller by overriding the
protected $defaultLimit = 25;
on the controller - pagination can also be passed via the url using
limit=xx&page=y
- pagination can also be limited to a max per page by overriding the
protected $maximumLimit = false;
parameter
- When Posting a new record, validation can be done by adding a
rulesForCreate
method to your controller returning an array eg
[
'email' => 'required|email',
'games' => 'required|numeric',
]
see https://laravel.com/docs/5.8/validation#conditionally-adding-rules
- for updating a record, add a method
rulesForUpdate
per above.
The following parameters are set in the Base Api controller and can be overwritten by your Controller on a case by case basis:
protected $resourceKeySingular = 'data';
Resource key for an item.protected $resourceKeyPlural = 'data';
Resource key for a collection.protected $defaultFields = ['*'];
Default Fields to respond withprotected $defaultSort = null;
Set the default sorting for queries.protected $defaultLimit = 25;
Number of items displayed at once if not specified. (0 = maximumLimit)protected $maximumLimit = 0;
Maximum limit that can be set via $_GET['limit']. - this ties in with the defaultLimit aswell, and if wanting to disable pagination , both should be 0. ) will allow all records to be returned in a single call.protected $unguard = false;
Do we need to unguard the model before create/update?
If you discover any security related issues, please email instead of using the issue tracker.