Laravel Livewire Tables Documentation

🎉 Enjoying this package? Consider sponsoring me on GitHub or buying me a beer.

This is the documentation for v2 but the latest version is v3. You can switch versions in the menu at the top. Check your current version with the following command:

composer show rappasoft/laravel-livewire-tables

Applying Filters

By default, a filter does nothing. You have to tell it how to process the selected value.

There are two ways to apply filters, at the filter level, and at the component level.

Apply Filters at the Filter Level

The filter() method will allow you to return a callback that will be used to process the selected value. You will get the builder and selected value to work with.

1SelectFilter::make('Active')
2 ->options([
3 '' => 'All',
4 '1' => 'Yes',
5 '0' => 'No',
6 ])
7 ->filter(function(Builder $builder, string $value) {
8 if ($value === '1') {
9 $builder->where('active', true);
10 } elseif ($value === '0') {
11 $builder->where('active', false);
12 }
13 }),

Apply Filters at the Component Level

If you don't want to apply at the filter level, you can apply the filter at the component level.

1public function builder(): Builder
2{
3 return User::query()
4 ->when($this->getAppliedFilterWithValue('active'), fn($query, $active) => $query->where('active', $active === 'yes'));
5}

You can use the getAppliedFilterWithValue() method to grab the current value of the filter or null if it is not applied.

Apply Filters at the Component Boot Level

You may wish to apply default filters i.e. current month on accessing the view in this case you can use:

1public function mount() {
2 $this->setFilter('created_after', date('Y-m-d', strtotime('now -1 month')));
3}

A note about integer values

Even if you have your values as strings, but are still using integers, you may have unexpected results when using Eloquent's when() method to apply your filters (if going that route).

For example, if you have values of 0 and 1, the eloquent when() method will not execute when the value is '0' as it treats it as false.

So it is better to not use getAppliedFilterWithValue() or integer keys in the situations where you want to apply the filter in the builder method.