Skip to main content

Filtering and sorting

Adding filters and sorters.

Index query

By default a resource will call YourModel::query() to get the results for your index page. You may override this method:

public function indexQuery()
{
return $this->model()::where('user_id', auth()->id());
}

Default sort

You can define a default sort column and sort order in your resource:

public function defaultSortColumn(): ?string
{
return 'created_at';
}

public function defaultSortDirection(): ?string
{
return 'descending';
}

Fields

Fields are filterable and sortable by default, you can disable this:

Select::make(__('Type'), 'type')
->options(
[
[
'value' => 'type_1',
'label' => __('Type 1')
],
[
'value' => 'type_2',
'label' => __('Type 2')
]
]
)
->sortable(false)
->filterable(false),

Options query

You can optionally override the options query for a resource. This query will be used for all relational fields that have a select field with options. This might be useful for optimizing queries since you don't have to eager load all relationships.

public function optionsQuery(): Builder
{
return $this->indexQuery();
}

Custom filter

You can override the default filter query:

->filterable()
->filterUsing(function($query, $value) {
$query->where('type', $value);
}),

Custom sort

You can override the default sort query:

->sortable()
->sortUsing(function($query, $direction) {
$query->orderBy('type', $direction);
})