Laravel Livewire Tables Documentation

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

This is the documentation for v1 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

Creating filters

Creating filters is not required, and the filters box will be hidden if none are defined.

To create filters, you return an array of Filter class objects from the filters() method.

The current types of filters are: select, multiSelect, date, and datetime (for supported browsers).

There are two steps to making a filter:

  1. Adding the Filter object to filters.
  2. Specifying how that filter acts on the query.

1. Adding the Filter object to filters.

1public function filters(): array
2{
3 return [
4 'type' => Filter::make('User Type')
5 ->select([
6 '' => 'Any',
7 User::TYPE_ADMIN => 'Administrators',
8 User::TYPE_USER => 'Users',
9 ]),
10 'active' => Filter::make('Active')
11 ->select([
12 '' => 'Any',
13 'yes' => 'Yes',
14 'no' => 'No',
15 ]),
16 'verified' => Filter::make('E-mail Verified')
17 ->select([
18 '' => 'Any',
19 1 => 'Yes',
20 0 => 'No',
21 ]),
22 'date' => Filter::make('Date')
23 ->date([
24 'min' => now()->subYear()->format('Y-m-d'), // Optional
25 'max' => now()->format('Y-m-d') // Optional
26 ]),
27 'tags' => Filter::make('Tags')
28 ->multiSelect([
29 'tag1' => 'Tags 1',
30 'tag2' => 'Tags 2',
31 'tag3' => 'Tags 3',
32 'tag4' => 'Tags 4',
33 ]),
34 ];
35}

When using the select box filter, the keys of the options you supply will be validated on select to make sure they match one of the options on the backend, otherwise it will be changed to null for safety.

String or integer keys are supported.

2. Specifying how that filter acts on the 'query'.

To apply the filter in your query, you first check its existence, and then just append some constraints.

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

As you can see we are just using the built-in Eloquent when method to check existence of our filter, and then apply the query.

2.1. Working with numeric keys:

If your filter has numeric keys, you may run into issues when you have a key that equals zero.

You will have to explicitly check:

1public function query(): Builder
2{
3 return User::with('attributes', 'parent')
4 ->when($this->getFilter('email'), fn ($query, $email) => $email === 'yes' ? $query->whereNotNull('email') : $query->whereNull('email'))
5 ->when($this->hasFilter('verified'), function ($query) {
6 if ($this->getFilter('verified') === 1) {
7 $query = $query->whereNotNull('email');
8 } else {
9 $query = $query->whereNull('email');
10 }
11 });
12}