Workflow
Filter Depends

This example demonstrates the implementation of a Interdependent Filter.

State
City
Alaska
Perryville
Alaska
Fort Wainwright
Alaska
New Stuyahok
Alaska
Buckland
Alaska
Cordova
Alaska
Naknek
Alaska
Teller
Alaska
Point Baker
Alaska
Kongiganak
Alaska
Salcha
Disclaimer: Table data is randomly generated for illustrative purposes only. The information here is not a reflection of the actual market and does not constitute business, financial, or medical advice.
<?php
 
namespace App\Livewire\Examples\FilterDependsTable;
 
use App\Models\City;
use App\Models\State;
use Illuminate\Database\Eloquent\Builder;
use PowerComponents\LivewirePowerGrid\Column;
use PowerComponents\LivewirePowerGrid\Facades\Filter;
use PowerComponents\LivewirePowerGrid\Footer;
use PowerComponents\LivewirePowerGrid\PowerGrid;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;
use PowerComponents\LivewirePowerGrid\PowerGridFields;
 
final class FilterDependsTable extends PowerGridComponent
{
public string $sortField = 'states.id';
 
public function setUp(): array
{
return [
 
Footer::make()
->showPerPage()
->showRecordCount(),
];
}
 
public function datasource(): ?Builder
{
return City::query()
->join('states', function ($state) {
$state->on('cities.state_id', '=', 'states.id');
})
->select('states.id as state_id', 'states.name as state_name', 'cities.name as city_name', 'cities.id as city_id');
}
 
public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('city_name')
->add('state_name');
}
 
public function columns(): array
{
return [
 
Column::make('State', 'state_name', 'states.id'),
 
Column::make('City', 'city_name', 'cities.name'),
 
];
}
 
public function filters(): array
{
return [
Filter::select('state_name', 'states.id')
->dataSource(State::all())
->optionLabel('name')
->optionValue('id'),
 
Filter::select('city_name', 'cities.id')
->depends(['state_id'])
->dataSource(
fn ($depends) => City::query()
->when(
isset($depends['state_id']),
fn (Builder $query) => $query->whereRelation(
'state',
fn (Builder $builder) => $builder->where('id', $depends['state_id'])
)
)
->get()
)
->optionLabel('name')
->optionValue('id'),
 
];
}
}
Code highlighting provided by Torchlight.dev

Here you can find all relevant packages installed on this demo.

Name
Version
Description
laravel/framework
v11
The Laravel Framework.
livewire/livewire
v3.4.12
A front-end framework for Laravel.
openspout/openspout
v4.24.0
PHP Library to read and write spreadsheet files (CSV, XLSX and ODS), in a fast and scalable way
power-components/livewire-powergrid
v5.6.3
PowerGrid generates Advanced Datatables using Laravel Livewire.