Страницы · Laravilt
Query Builder
Apply request-driven filters and sorting to any Eloquent query.
laravilt/query-builder is a small, standalone helper. It applies filter values and sorting to an Eloquent Builder, and serializes its configuration to Inertia props so a frontend can render the controls. Use it for custom pages and endpoints. Resource tables have their own table filters.
Example
apply() skips filters whose value is null or '', and returns the same builder so you can keep chaining.
Search
Register the columns to search with searchable(), and pass the term with search():
searchable()and search support require Laravilt v1.1 or later.
apply() matches the term against every searchable column with LIKE '%term%', OR'd together inside a single where group, so it combines safely with your filters. The term is trimmed, and %, _, and \ in it are escaped, so they match literally. An empty term, or no searchable columns, adds no constraint.
Use dot notation to search a column on a relation. author.name becomes orWhereHas('author', ...), and nested paths such as author.company.name work too. If the part before the last dot isn't a relation on the model, the whole string is used as a column name.
searchable() also accepts the columns as separate arguments: ->searchable('title', 'body'). Each call replaces the previous list.
Methods
| Method | Description |
|---|---|
filters(array), addFilter(Filter) | Register filters |
sorts(array), addSort(Sort) | Register sort options |
applyFilters(array) | Filter values keyed by filter name |
sortBy(?string $column, ?string $direction = 'asc') | Active sort (invalid directions become asc) |
searchable(array|string) | Columns the search term is matched against. See Search |
search(?string) | Search term, matched against the searchable() columns |
perPage(int), paginated(bool) | Pagination settings passed to the frontend |
apply(Builder) | Apply filters and sorting to a query |
toInertiaProps() | Serialize filters, sorts, and current values |