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%' |
You can pass to the filters an array of values
ie: filter[user_id]=1||2||||4||7
or filter[user_id!]=55||33
If you need to filter on whether a field is null or not null, you can use the filter param as of version 1.23.0 EG: filter[age]=NULL
or filter[age!]=NULL
. Note that NULL must be uppercase.
Older versions Add a scope to your model: eg
public function scopeAgeNull(Builder $builder, $isNull = true){
$isNull ? $builder->whereNull('age') : $builder->whereNotNull('age');
}
Add to your allowedScopes and can then be called in url as ?ageNull=1
for where null and ?ageNull=0
for where age not null
In addition to filtering, you can use Laravel's Eloquent Query Scopes to do more complex searches or filters.
Simply add an $allowedScopes
to your ApiResource
, and that scope will be exposed as a query parameter.
Assuming you have a scopeFullname
defined on your Eloquent Model, you can expose this scope to your API as follows:
protected static $allowedScopes = [
'fullname'
];
Given the above $allowedScopes
array, your API consumers will now be able to request ?fullname=John
. The query parameter value will be passed to your scope function in your Eloquent Model.
You can easily filter using any related model that is configured for include
. Simply specify ?filter[model.field]=123
in your query string. The same filter options above apply to related fields.
By default all fields are returned, you can limit that to specific fields in the following ways:
$defaultFields
default as protected $defaultFields = ['*'];
- switch to include an array of fieldsfields=id,name,age
= will only return those, this will also override the above.addfields
and removefields
params in url querystring will work with these.$appends
property to automatically include custom attribute accessors.include=join1,join2
which will return those joins (one or many).Simply add a protected static $mapResources
to your Resource
to define which resources to assign your related data. E.e., for a one to many relationship, you should specify a collection, and a one-to-one relationship specify the related resource directly. This will allow the API to properly format the related record.
protected static $mapResources = [
'notes' => NotesCollection::class,
'owner' => OwnerResource::class
];
For BelongsToMany
or MorphToMany
relationships, you can choose the sync strategy. By default, this will take an additive strategy. That is to say, related records sent will be ADDED to any existing related records. On a request-by-request basis, you can opt for a sync strategy which will remove the pivot for any related records not listed in the request. Note the actual related record will not be removed, just the pivot entry.
To opt for the sync behavaiour, set ?sync[field]=true
in your request.
sort=age asc
or sort=age asc,name desc,eyes
- generates sql of sort age asc
and sort age asc, name desc, eyes asc
respectivelyprotected $defaultSort = null;
parameterprotected $defaultLimit = 25;
on the controllerlimit=xx&page=y
protected $maximumLimit = false;
parameterrulesForCreate
method to your controller returning an array eg[
'email' => ['required', 'email'],
'games' => 'required|numeric',
]
A better solution would be to use the Form Request Validation