Custom Responses
The package renders each row from the plan of the search: its selects, gates, aggregates and includes. You can change each row before it is sent, and add keys to the meta of a search page.
The response envelope
A search answers a page, and destroy, restore and forceDestroy answer a list:
{"current_page": 1, "data": [{"id": 1, "title": "Dune"}], "from": 1, "last_page": 1, "per_page": 50, "to": 1, "total": 1, "meta": {}}
{"data": [{"id": 5, "title": "Dune"}], "meta": {}}
metais always an object,{}when it holds nothing. When the search asks for thecreategate,metaholds its answer under the gates key.- The pages of a resource that sets
$maxTotaladdtotal_cappedaftertotal, the pages of a search engine adddropped, and a search that asks for totals addstotals. See Search overview. - Each field value comes in the type that the OpenAPI document gives it: a
NumberorIntegerfield turns a numeric string, such as the value of adecimal:2cast, into a number ("25000.50"becomes25000.5), and aJsonfield returns an empty array as{}.map()receives the converted values. See Fields.
Changing the rows with map()
Generate a response class:
php artisan rest:response ArticleResponse
namespace App\Rest\Responses;
use Illuminate\Database\Eloquent\Model;
use Lomkit\Rest\Http\Response as RestResponse;
class ArticleResponse extends RestResponse
{
/**
* @param array<string, mixed> $responseModel
* @return array<string, mixed>
*/
#[\Override]
protected function map(Model $model, array $responseModel): array
{
return [
...$responseModel,
'excerpt' => str($model->body)->limit(120)->toString(),
];
}
}
Set it on the resource:
public static string $response = \App\Rest\Responses\ArticleResponse::class;
map()receives each row of the page or of the list, and each record of a mutation'sresponseblock, as the package rendered it. It returns the row to send.map()reads the columns that the search loaded. A column that the row does not return, such asbodyabove, goes in the resource'salwaysSelect(): reading a column that was not loaded throwsLomkit\Rest\Exceptions\MissingColumnException. See Sorts and selects.- The gates of a row reach
map()as an object:$responseModel['gates']->authorized_to_view['allowed']. map()runs on the rows of the endpoint and on the rows thatRest::search()renders. A subclass that overridestoResponse()changes the endpoint response only.
map() can send any attribute of the model. Send only what the fields of the resource allow the user to read.Adding meta to a search page
searchMeta() on the resource adds keys to the meta of a search page, after the create gate:
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Lomkit\Rest\Search\SearchContext;
public function searchMeta(SearchContext $search, LengthAwarePaginator $page): array
{
return ['read_at' => now()->toIso8601String()];
}
- It runs after the
searched()event, with the same page, on the database and on a search engine. - It reads the columns of the rows under the same check as
map(). - It does not run for the rows of an action, a mutation or a deletion.
- Returning the gates key or
instructionsthrowsLomkit\Rest\Exceptions\InvalidResourceException.
An instruction at the root of the search adds its own object under meta.instructions, keyed by the alias of its entry or else its uriKey, with searchMeta(InstructionFields $fields). An instruction that returns [] adds nothing:
{"meta": {"read_at": "2026-12-01T10:00:00+00:00", "instructions": {"last-twelve-months": {"notice": "Results limited to the last 12 months."}}}}
Rendering outside HTTP
Rest::search() renders the same body as the endpoint, with map() and searchMeta(). See Searching outside HTTP.
Events and Hooks
Run your code at fixed moments of each endpoint with resource events and row hooks, and listen to the Laravel events of each write.
OpenAPI Documentation
Document every endpoint of your resources in OpenAPI with Scramble, with typed bodies, responses and examples, and keep the document under test.