Searching outside HTTP
Rest::search() runs the search of a resource from PHP, for a given user, as the search endpoint runs it. Use it to replay a search payload that a client built: a saved search that a scheduled job sends each morning, a console command that reruns it, or an export of what a user sees. To read rows that your own code chooses, keep using Eloquent.
use Lomkit\Rest\Facades\Rest;
$search = Rest::search(TicketResource::class)
->as($user)
->payload($savedSearch);
$page = $search->page(); // LengthAwarePaginator of the models
$body = $search->render(page: 2); // the endpoint body, as an array
$rows = $search->lazy(); // every rendered row, by chunks
The builder
payload()takes the value of thesearchkey of the endpoint body. It is validated with the rules and limits of the endpoint. A failure throwsIlluminate\Validation\ValidationExceptionwith the keys and messages of the endpoint, such assearch.filters.0.field. Withoutpayload(), the search runs as an endpoint body withoutsearch.as()is required. Without it, the search throwsSearchRunException:The search of the resource [App\Rest\Resources\TicketResource] has no user. Call as() with the user, or as(null) to search as a guest.as(null)searches as a guest, also when a user is logged in.as($user, 'api')names the guard of the user; the default guard applies otherwise.- The given user is the one that
$this->request()->user()returns inperimeterQuery(), in the declarations of the resources and in their hooks, at every include level. The policies and the decision cache use that user and guard. The logged-in user and the default guard of the application do not change. viewAnyis checked before the search, as on the endpoint: a denial throwsLomkit\Rest\Exceptions\AuthorizationDenied.
page() and render()
use Illuminate\Support\Facades\Mail;
use Lomkit\Rest\Facades\Rest;
public function handle(): void
{
$page = Rest::search(TicketResource::class)
->as($this->user)
->payload($this->savedSearch->payload)
->page();
Mail::to($this->user)->send(new OpenTickets($page->items(), $page->total()));
}
page()returns theIlluminate\Contracts\Pagination\LengthAwarePaginatorof the models of the page, with their includes cut to their limits.render()returns the body of the endpoint as an array:json_encode($body)is the content that the endpoint answers for the same user and payload,metaincluded.- The objects that stay
{}when empty, such asmeta,pivotandgates, arestdClassinstances in that array. page(page: 2)andrender(page: 2)read that page instead of thepageof the payload. It is validated assearch.page.page()andrender()runsearching()before the query andsearched()after it, as on the endpoint.page()andrender()are bounded byrest.limits.sizes.rows, as a page of the endpoint:The search field takes the response over the limit of 49 rows: it can render up to 50 rows.for a page of 50 rows with the limit set to 49.lazy()does not count rows. See Response rows.- The values of a row follow the type of their field, as on the endpoint: a
Numbervalue is a number, and an emptyJsonvalue is an object. See Value types in rows. - A payload that asks for totals needs
render(), which returns them under the roottotalskey.page()throwsSearchRunException:The search of the resource [App\Rest\Resources\TicketResource] asks for totals, which page() cannot return: a paginator holds the rows only. Call render(), which returns the totals at the root of the page, or send the search without totals. - A payload with
"source": "engine"runs on the search engine of a resource that serves its lists, and givesThe search.source field is prohibited.elsewhere.
lazy() and exports
lazy() reads every row of the search. Here, an export of the tickets that the user sees, with the filters of the client, streamed in the request:
use Illuminate\Http\Request;
use Lomkit\Rest\Facades\Rest;
use Symfony\Component\HttpFoundation\StreamedResponse;
public function __invoke(Request $request): StreamedResponse
{
$rows = Rest::search(TicketResource::class)
->as($request->user())
->payload($request->input('search', []))
->lazy();
return response()->streamDownload(function () use ($rows): void {
$file = fopen('php://output', 'w');
foreach ($rows as $row) {
fputcsv($file, [$row['id'], $row['title'], $row['status']]);
}
fclose($file);
}, 'tickets.csv');
}
{
"search": {
"filters": [{"field": "status", "value": "open"}],
"selects": [{"field": "id"}, {"field": "title"}, {"field": "status"}],
"sorts": [{"field": "id"}]
}
}
lazy(chunk: 500, max: null)returns anIlluminate\Support\LazyCollectionof rendered rows. Each row is the row that the endpoint returns indata: its fields, its includes cut to their limits with@more, and thegatesthat the payload asks for.- In order,
lazy()returns the rows of every page of the endpoint, each row once. - When
lazy()is called, it validates the payload, checksviewAny, runssearching()and reads the keys of the rows in the order of the search. A validation error or a denial throws there, before the streamed response starts. - The rows load and render by chunks of
chunkkeys while you iterate, with their includes and aggregates. The query of the keys selects the model key only, so only the keys of all rows stay in memory. - A row deleted after its key was read is skipped. Each iteration loads the rows again.
maxreads the firstmaxrows only.lazy()ignores thelimitand thepageof the payload.searched()does not run, and an override ofpaginate()in the resource does not apply tolazy().
Searches on a search engine
lazy() also reads a search that runs on a search engine: a text search on a ScoutEngine resource, and "source": "engine" on a resource that serves its lists. source keeps the 422 of page() on the other resources, and "source": "database" streams in SQL.
- When
lazy()is called, it reads every key of the search from the engine, whateverchunk: in one page on the Scoutcollection,databaseandnulldrivers, and elsewhere in pages of the largest size that the engine takes, 250 on Typesense and 1,000 on Meilisearch, Algolia, Turbopuffer and the profiles that extensions provide. - While you iterate, the rows load by chunks of
chunkkeys, in the order of the engine, each once, as the rows of an engine page: with the global scopes of the model andsearch.trashed, withoutperimeterQuery()and without theRestrictsQueriesrestrictions of the extensions.lazy()returns the rows that the pages of the endpoint show, throughScoutEngine::perimeter()and the engine restrictions, which apply when the keys are read. - A row whose key was read and that leaves the perimeter during a long stream is still streamed, as a page loaded before the change would show it. A row deleted since indexing is skipped.
- Only the keys stay in memory, as in SQL, and
maxcaps the keys read from the engine.
Every refusal throws when lazy() is called, before a streamed response starts. Both exceptions below extend SearchRunException, so a catch (SearchRunException) keeps catching them. Catch them apart to answer each one:
try {
$rows = $search->lazy();
} catch (EngineResultsChangedException) {
$rows = $search->lazy();
} catch (EngineWindowException $e) {
// narrow the search, or lazy(max: $e->window ?? 1000)
}
Lomkit\Rest\Exceptions\EngineWindowExceptionwhen the search can match more results than the engine returns, andmaxdoes not fit within that bound: the result window (10,000 on Turbopuffer) or the reach that the engine reports (pagination.maxTotalHitson Meilisearch,paginationLimitedToon Algolia). It is also thrown when the engine does not report its reach, because the key of the application cannot read the settings of the index, andlazy()has nomax. Itswindowholds the bound that refused the search,nullwhen the engine reports none.Lomkit\Rest\Exceptions\EngineResultsChangedExceptionwhen a key comes back twice or the total changes while the keys are read: calllazy()again, and setscout.after_committotrueso that the index changes once the transactions commit.- On Turbopuffer, whose total does not count the text, the keys are read until a page is not full: a row that moves from one page to another while they are read cannot be detected there. The window still bounds the walk: when the keys read reach its 10,000 results and
maxis absent or above it,lazy()throwsEngineWindowException.
lazy() errors
All of them throw Lomkit\Rest\Exceptions\SearchRunException or one of its subclasses:
| Case | Message |
|---|---|
| The search can match more results than the engine returns | The search of the resource [App\Rest\Resources\TicketResource] runs on a search engine, which returns at most its first 10000 results, and the search can match more. Give lazy() a max of at most 10000, narrow the search, or read the pages with page(). |
The engine does not report its reach, and lazy() has no max | The search of the resource [App\Rest\Resources\TicketResource] runs on a search engine that does not report how many results it can return. lazy() reads every row: give lazy() a max, or read the pages with page(). |
| The results changed while the keys were read | The search engine results of the resource [App\Rest\Resources\TicketResource] changed while lazy() read their keys. Call lazy() again, and set scout.after_commit to true so that the index changes once the database transactions commit. |
A chunk below 1 | The chunk of lazy() must be at least 1, [0] given. |
A negative max | The max of lazy() must be at least 0, [-1] given. |
lazy() refuses totals with a ValidationException: The search.totals field is prohibited. See Totals.