Digging Deeper

Custom Responses

Change the rows that a resource sends with map(), and add keys to the meta of a search page with searchMeta().

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": {}}
  • meta is always an object, {} when it holds nothing. When the search asks for the create gate, meta holds its answer under the gates key.
  • The pages of a resource that sets $maxTotal add total_capped after total, the pages of a search engine add dropped, and a search that asks for totals adds totals. See Search overview.
  • Each field value comes in the type that the OpenAPI document gives it: a Number or Integer field turns a numeric string, such as the value of a decimal:2 cast, into a number ("25000.50" becomes 25000.5), and a Json field 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's response block, 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 as body above, goes in the resource's alwaysSelect(): reading a column that was not loaded throws Lomkit\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 that Rest::search() renders. A subclass that overrides toResponse() 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 instructions throws Lomkit\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."}}}}

See Scopes and instructions.

Rendering outside HTTP

Rest::search() renders the same body as the endpoint, with map() and searchMeta(). See Searching outside HTTP.