Exposed Data
Fields
Each Rest resource contains a fields
method. This method returns an array of strings specifying the columns you want to expose. This fields are accessible in terms of selecting and mutating.
To add a field to a resource, you may simply add it to the resource's fields
method.
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
return [
'id',
'name',
];
}
Conditional field exposing
You might want to choose the fields you are exposing depending on the user. You can achieve this by conditioning the fields
method:
public function fields(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
$fields = [
'id',
'name'
];
if ($request->user()->isAdministrator()) {
array_push($fields, 'password');
}
return $fields;
}
Sorts
The fields allowed are the one you define in the fields
method
Default sort
By default, Rest Api sorts by id
descending, if you want to change this, extend the defaultOrderBy
method.
/**
* Return the default ordering for resource queries.
*
* @param RestRequest $request
*
* @return array
*/
public function defaultOrderBy(RestRequest $request): array
{
return [
'id' => 'desc',
];
}
Scopes
Each Rest resource contains a scopes
method. This method returns an array of strings specifying the scopes you want to expose.
To add a scope to a resource, you may simply add it to the resource's scopes
method.
public function scopes(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
return [
'withTrashed'
];
}
Conditional scope exposing
You might want to choose the scopes you are exposing depending on the user. You can achieve this by conditioning the scopes
method:
public function scopes(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
$scopes = [
'withTrashed'
];
if ($request->user()->isAdministrator()) {
array_push($scopes, 'numbered');
}
return $scopes;
}
Limits
Each Rest resource contains a limits
method. This method returns an array of numbers specifying the limits you want to allow.
To add a limit to a resource, you may simply add it to the resource's limits
method.
public function limits(\Lomkit\Rest\Http\Requests\RestRequest $request) {
return [
10,
25,
50
];
}
Conditional limits specifying
You might want to choose the limits you are exposing depending on the user. You can achieve this by conditioning the limits
method:
public function limits(\Lomkit\Rest\Http\Requests\RestRequest $request)
{
$limits = [
10,
25,
50
];
if ($request->user()->isAdministrator()) {
array_push($limits, 1000);
}
return $limits;
}
Basics
The primary feature of Laravel Rest Api is the ability to access and administrate your database records using Eloquent. Laravel Rest Api accomplishes this by allowing you to define a Rest "resource" that corresponds to each Eloquent model in your application.
Relationships
In addition to the variety of fields we've already discussed, Laravel Rest Api has full support for all of Laravel's relationships. After adding relationships to your REST resources, you'll be able to directly access them from the requested resource, eliminating the need for multiple API calls.