Querying
Filters
Write filters, register them on a Resource, and let fields generate filters for you.
A filter narrows the rows of a Resource's table. The user types a token such as status:paid in the filter bar, and Lathe applies the filter's apply() method to the query.
Before you write a filter class, check whether a filterable field covers the need. Most columns do not need custom code.
Create a filter
lathe:filter writes a filter class to app/Lathe/Filters:
php artisan lathe:filter UserType
php artisan lathe:filter PriceRange --type=range
--type is select (the default), boolean, date or range. --custom is deprecated: it writes a filter with its own React component, which the filter bar does not draw (see Custom components).
Every filter extends Lathe\Filters\Filter or one of its subclasses and implements apply():
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
Lathe calls apply() only when the user has chosen a value. The shape of $value depends on the filter type.
Select filters
A select filter shows one dropdown. options() returns the choices as label => value. Lathe passes the chosen value to apply().
namespace App\Lathe\Filters;
use Illuminate\Database\Eloquent\Builder;
use Lathe\Filters\Filter;
use Lathe\Http\Requests\LatheRequest;
class UserType extends Filter
{
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
{
return $query->where($query->qualifyColumn('type'), $value);
}
public function options(LatheRequest $request): array
{
return [
'Administrator' => 'admin',
'Editor' => 'editor',
'Subscriber' => 'subscriber',
];
}
}
Use qualifyColumn() so the filter still works when the Resource's indexQuery() joins another table.
Call multiple() to let the user pick several options. $value is then a list. The filter bar needs it for a token such as status:pending,paid.
Boolean filters
A boolean filter shows a checkbox per option. Extend Lathe\Filters\BooleanFilter. $value maps each option to true or false:
use Lathe\Filters\BooleanFilter;
class UserFeatures extends BooleanFilter
{
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
{
if ($value['is_admin'] ?? false) {
$query->where('is_admin', true);
}
if ($value['is_verified'] ?? false) {
$query->whereNotNull('email_verified_at');
}
return $query;
}
public function options(LatheRequest $request): array
{
return [
'Admins' => 'is_admin',
'Verified' => 'is_verified',
];
}
}
Date filters
A date filter picks a range of days. Extend Lathe\Filters\DateFilter. $value holds from and to as Y-m-d strings. Either side is null when the user leaves it empty. Lathe refuses a date such as 2026-02-30.
use Lathe\Filters\DateFilter;
class CreatedBetween extends DateFilter
{
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
{
if ($value['from'] !== null) {
$query->whereDate('created_at', '>=', $value['from']);
}
if ($value['to'] !== null) {
$query->whereDate('created_at', '<=', $value['to']);
}
return $query;
}
}
whereDate() compares the date as the database stores it: a datetime column holds wall time in the app's time zone, app.timezone.
column() names the date column the filter narrows. It is optional, and null by default. When it names one, a dashboard card dated by that column opens this list with the filter set to the card's days (see Open records). The card counts days in the user's time zone, so a filter that names its column cuts its days there too, or the list and the card disagree about the rows near midnight. Timezone::dayBounds() gives a local day's start and end in the app's time zone, the zone the column holds:
use Lathe\Filters\DateFilter;
use Lathe\Support\Timezone;
class PlacedBetween extends DateFilter
{
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
{
$timezone = app(Timezone::class);
$zone = $timezone->forRequest($request);
if ($value['from'] !== null) {
$query->where('placed_at', '>=', $timezone->dayBounds($value['from'], $zone)[0]);
}
if ($value['to'] !== null) {
$query->where('placed_at', '<', $timezone->dayBounds($value['to'], $zone)[1]);
}
return $query;
}
public function column(): ?string
{
return 'placed_at';
}
}
Range filters
A range filter shows a minimum and a maximum input. Extend Lathe\Filters\RangeFilter. $value holds min and max as numbers, or null for an empty side.
use Lathe\Filters\RangeFilter;
class PriceRange extends RangeFilter
{
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
{
if ($value['min'] !== null) {
$query->where('price', '>=', $value['min']);
}
if ($value['max'] !== null) {
$query->where('price', '<=', $value['max']);
}
return $query;
}
}
For an integer column, call integers() on the filter. It refuses 10.5 and 1e20, which PostgreSQL would reject with a server error.
In the filter bar, price:>10 and price:<10 on this filter include 10: they write min and max, and the chip reads "≥ 10" or "≤ 10". To make them leave out 10, set $strict and read the strict sides gt and lt, which are absent when the URL does not send them:
class PriceRange extends RangeFilter
{
protected bool $strict = true;
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
{
return $query
->when($value['min'] !== null, fn (Builder $q) => $q->where('price', '>=', $value['min']))
->when($value['max'] !== null, fn (Builder $q) => $q->where('price', '<=', $value['max']))
->when(isset($value['gt']), fn (Builder $q) => $q->where('price', '>', $value['gt']))
->when(isset($value['lt']), fn (Builder $q) => $q->where('price', '<', $value['lt']));
}
}
A filter that filterable() makes from a number or currency field takes the strict sides already. One with a filterable(fn ...) callback does not: the callback receives min and max only, and the filter bar writes >n and <n as min and max, as for a subclass that leaves $strict false.
Register filters
Return the filters from the Resource's filters() method:
use App\Lathe\Filters\CreatedBetween;
use App\Lathe\Filters\UserType;
use Lathe\Http\Requests\LatheRequest;
public function filters(LatheRequest $request): array
{
return [
new UserType,
new CreatedBetween,
];
}
A lens has its own filters() method, separate from the Resource's.
Name, key and default
Lathe builds the filter's title from the class name: UserType becomes "User Type". Override name() to change it. The key() is the kebab-case class name, user-type, and appears in the URL. Override it when two instances of one class need different keys.
public function name(): string
{
return 'Account type';
}
public function key(): string
{
return 'account-type';
}
default() returns the value a filter starts with. It returns null unless you override it:
public function default(): mixed
{
return 'admin';
}
Reuse one filter class
A constructor argument lets one class filter several columns. Give each instance its own key:
class TimestampBetween extends DateFilter
{
public function __construct(private readonly string $column)
{
//
}
public function apply(LatheRequest $request, Builder $query, mixed $value): Builder
{
return $query
->when($value['from'] !== null, fn (Builder $q) => $q->whereDate($this->column, '>=', $value['from']))
->when($value['to'] !== null, fn (Builder $q) => $q->whereDate($this->column, '<=', $value['to']));
}
public function key(): string
{
return 'between-'.$this->column;
}
public function name(): string
{
return str($this->column)->headline()->toString();
}
}
public function filters(LatheRequest $request): array
{
return [new TimestampBetween('created_at'), new TimestampBetween('verified_at')];
}
Where and when a filter applies
On a Resource index, its lenses and a relation list on a record page, the filter bar applies a filter when its chip is made, 300 ms after the last change.
immediate() and placement() are deprecated and change nothing in the filter bar. Only @lathephp/core's deprecated FilterMenu reads immediate(), and nothing reads placement(). Both still return the filter, so a Resource that calls them keeps working through 1.x.
Large option lists
A select filter loads its options inline until the list holds more than limits.filter_remote_threshold (100) entries. After that, the filter becomes a remote picker. The picker types ahead and loads one page at a time through searchOptions(). A Resource changes the threshold with filterRemoteThreshold().
Force a remote picker with searchable() or the #[Searchable] attribute. A closure decides per request:
use Lathe\Attributes\Filter\Searchable;
#[Searchable]
class Country extends Filter
{
// ...
}
(new Country)->searchable(fn ($request) => $request->user()->isAdmin())
The default searchOptions() reads options() and matches the start of each label. A remote filter over a big table should override it with a query, and acceptsRemoteValue() to refuse a value its column cannot hold. The workbench's OrderCustomer filter does both.
Filterable fields
Any field can generate a filter on the Resource's table. Call filterable() on the field:
Field::number('Quantity')->filterable(),
Field::select('Status')
->options(['pending', 'paid', 'shipped'])
->filterable(),
Field::currency('Total', 'total_cents')->asMinorUnits()->filterable(scale: 100),
The signature is filterable(bool|Closure $filterable = true, ?array $options = null, int $scale = 1).
- Without
$options, a select field gets a select filter of its own options, and any other field gets a range filter with a minimum and a maximum. A select with remote options gets the range filter too. - With
$options, the field gets a select filter of those values. Pass a list of values, orvalue => label. scalelets the inputs take the shown amount while the URL and the query keep the stored value. Use100for cents.- A
belongsTo,belongsToManyormorphTofield gets a relation filter. It lists related records and goes remote afterlimits.filter_remote_thresholdrows. AmorphTofilter lists its types.
Generated filters have the key field_{attribute}.
A closure takes over the query. A computed field needs one, because Lathe cannot guess a column. Without a closure, filterable() on a computed field throws a LogicException.
use Illuminate\Database\Eloquent\Builder;
Field::text('Full name')->filterable(
fn (LatheRequest $request, Builder $query, mixed $value, ?string $attribute) => $query
->where('first_name', 'like', $value.'%')
->orWhere('last_name', 'like', $value.'%'),
)
Who can see a filter
canSee() hides a filter from some users. A request that applies a hidden filter answers 422.
public function filters(LatheRequest $request): array
{
return [
(new UserType)->canSee(fn ($request) => $request->user()->isAdmin()),
];
}
How filters reach the server
The filter bar writes the filters into the URL in plain form, one parameter per filter key:
?filter[status]=paid
?filter[tag][]=a&filter[tag][]=b
?filter[total][min]=10&filter[total][max]=50
?filter[total][gt]=10
?filter[placed][from]=2026-01-01
Lathe checks every value before apply() runs. An unknown key, a hidden filter or a value of the wrong shape answers 422. Saved tabs, exports and bulk selections read the same parameters, so a filter works everywhere the table does.
Custom components
A filter with its own React component is deprecated, with lathe:filter --custom, filter() and FilterMenu of @lathephp/core. They keep working through 1.x, but the filter bar does not draw the component: it shows such a filter as a text key, where the user types its key and a value, as sentiment:happy, and the chip reads "Sentiment: happy"; a value its options() does not list is refused. Only a custom page that renders FilterMenu still draws the component. For a new filter, extend a select, boolean, date or range filter instead.
--custom writes a filter class and a React component, and says the option is deprecated:
php artisan lathe:filter Sentiment --custom
The class returns the name of the component from component(). The generated component sits in resources/js/lathe/filters/Sentiment.tsx and registers itself with filter() from @lathephp/core:
public function component(): string
{
return 'sentiment-filter';
}
import { filter, type FilterComponentProps } from '@lathephp/core';
function Sentiment({ filter: definition, value, onChange }: FilterComponentProps) {
return (
<input
type="text"
aria-label={definition.name}
value={typeof value === 'string' ? value : ''}
onChange={(event) => {
onChange(event.target.value);
}}
/>
);
}
filter('sentiment-filter', Sentiment);
The component receives the filter definition and the current value. It calls onChange with the new value. A filter that extends Filter takes a string. When options() lists choices, Lathe refuses any other value before it calls apply().
What you see
Click the filter bar above the index table and a list of the Resource's filters opens. Choose Status, then a value, and a chip reads "Status: Paid". The table shows only the matching rows and the address bar carries the choice, for example ?filter[status]=paid. Reload the page and the same chip and rows return. A relation list on a record page has the same filter bar, and keeps its chips until the page reloads.