Filament page filter

I have this code where I have tried to pass date filter in my filament page. I made one similar type of report few months ago which works with this similar approad but this one is not working. Even when I pass the date filter the data is not changing in the view but the date is passing through. https://gist.github.com/Namrata199/b913be06177c0b1317d334180e3826ce
Gist
Filament Page Data not changing
Filament Page Data not changing. GitHub Gist: instantly share code, notes, and snippets.
22 Replies
LeandroFerreira
LeandroFerreira6mo ago
Livewire
Lifecycle Hooks | Livewire
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
namrata
namrata6mo ago
I used mount but it did not work. The problem is the data doesnot change even when I apply the date filter
namrata
namrata6mo ago
LeandroFerreira
LeandroFerreira6mo ago
->modifyQueryUsing(fn () => $this->apiData)
->modifyQueryUsing(fn () => $this->apiData)
?
namrata
namrata6mo ago
where do i do this ?
protected function getTableQuery(): Builder
{
return Product::where('active', true)->publicDisplay()
->orderBy('orders_count', 'desc')->modifyQueryUsing(fn () => $this->apiData);
}
protected function getTableQuery(): Builder
{
return Product::where('active', true)->publicDisplay()
->orderBy('orders_count', 'desc')->modifyQueryUsing(fn () => $this->apiData);
}
this gives error
namrata
namrata6mo ago
No description
LeandroFerreira
LeandroFerreira6mo ago
are you using Filament v3?
namrata
namrata6mo ago
yes
Solution
LeandroFerreira
LeandroFerreira6mo ago
https://filamentphp.com/docs/3.x/tables/adding-a-table-to-a-livewire-component#adding-the-table
public function table(Table $table): Table
{
return $table
->query(Product::query())
->modifyQueryUsing(fn () => $this->apiData)
...;
}
public function table(Table $table): Table
{
return $table
->query(Product::query())
->modifyQueryUsing(fn () => $this->apiData)
...;
}
namrata
namrata6mo ago
Thanks a lot this solved the data issue but since I am returning a collection,
private function getData($startDate, $endDate)
{
$cartItems = CartItem::where('order_id', '<>', 0)->where('created_at', '>=', $startDate)
->where('created_at', '<=', $endDate)
->selectRaw('product_id, count(*) as orders_count')
->groupBy('product_id')
->orderBy('orders_count', 'desc')
->get();

return $cartItems->map(function ($cartItem) {
return [
'name' => $cartItem->product->name,
'orders_count' => $cartItem->orders_count,
];
});
}
private function getData($startDate, $endDate)
{
$cartItems = CartItem::where('order_id', '<>', 0)->where('created_at', '>=', $startDate)
->where('created_at', '<=', $endDate)
->selectRaw('product_id, count(*) as orders_count')
->groupBy('product_id')
->orderBy('orders_count', 'desc')
->get();

return $cartItems->map(function ($cartItem) {
return [
'name' => $cartItem->product->name,
'orders_count' => $cartItem->orders_count,
];
});
}
here the table part crashes.
No description
namrata
namrata6mo ago
so there is no way ti get the data I want ? with my query ?
LeandroFerreira
LeandroFerreira6mo ago
If it returns an Eloquent Builder, it will work
namrata
namrata6mo ago
I changed it to retrun this
private function getData($startDate, $endDate)
{
$cartItemsQuery = CartItem::where('order_id', '<>', 0)
->where('created_at', '>=', $startDate)
->where('created_at', '<=', $endDate)
->selectRaw('product_id, count(*) as orders_count')
->groupBy('product_id')
->orderBy('orders_count', 'desc');

return $cartItemsQuery;
}
private function getData($startDate, $endDate)
{
$cartItemsQuery = CartItem::where('order_id', '<>', 0)
->where('created_at', '>=', $startDate)
->where('created_at', '<=', $endDate)
->selectRaw('product_id, count(*) as orders_count')
->groupBy('product_id')
->orderBy('orders_count', 'desc');

return $cartItemsQuery;
}
No description
LeandroFerreira
LeandroFerreira6mo ago
try product_id as id in the select
namrata
namrata6mo ago
gives this
No description
LeandroFerreira
LeandroFerreira6mo ago
share the whole code
namrata
namrata6mo ago
public function table(Table $table)
{
return $table
->query(Product::query()->where('active', true)->publicDisplay())
->modifyQueryUsing(fn () => $this->apiData)
->columns([
TextColumn::make('name'),
TextColumn::make('orders_count'),
])->defaultSort('orders_count', 'desc')
->filters([
Filter::make('filters')
->form(
[
DatePicker::make('start_at')->default(now()),
DatePicker::make('end_at')->default(now()),
]
)->columns(2)
->columnSpan(2)
], \Filament\Tables\Enums\FiltersLayout::AboveContent)
->actions([
// ...
])
->bulkActions([
// ...
]);
}

public function updated($name): void
{
if (Str::of($name)->contains('tableFilters.filters')) {

$startDate = data_get($this->tableFilters, 'filters.start_at');
$endDate = data_get($this->tableFilters, 'filters.end_at');

$this->apiData = $this->getData($startDate, $endDate);
}
}

private function getData($startDate, $endDate)
{
$cartItemsQuery = CartItem::where('order_id', '<>', 0)
->where('created_at', '>=', $startDate)
->where('created_at', '<=', $endDate)
->selectRaw('product_id as id, count(*) as orders_count')
->groupBy('product_id')
->orderBy('orders_count', 'desc');

return $cartItemsQuery;
}
public function table(Table $table)
{
return $table
->query(Product::query()->where('active', true)->publicDisplay())
->modifyQueryUsing(fn () => $this->apiData)
->columns([
TextColumn::make('name'),
TextColumn::make('orders_count'),
])->defaultSort('orders_count', 'desc')
->filters([
Filter::make('filters')
->form(
[
DatePicker::make('start_at')->default(now()),
DatePicker::make('end_at')->default(now()),
]
)->columns(2)
->columnSpan(2)
], \Filament\Tables\Enums\FiltersLayout::AboveContent)
->actions([
// ...
])
->bulkActions([
// ...
]);
}

public function updated($name): void
{
if (Str::of($name)->contains('tableFilters.filters')) {

$startDate = data_get($this->tableFilters, 'filters.start_at');
$endDate = data_get($this->tableFilters, 'filters.end_at');

$this->apiData = $this->getData($startDate, $endDate);
}
}

private function getData($startDate, $endDate)
{
$cartItemsQuery = CartItem::where('order_id', '<>', 0)
->where('created_at', '>=', $startDate)
->where('created_at', '<=', $endDate)
->selectRaw('product_id as id, count(*) as orders_count')
->groupBy('product_id')
->orderBy('orders_count', 'desc');

return $cartItemsQuery;
}
LeandroFerreira
LeandroFerreira6mo ago
try to share the whole code in gist please
namrata
namrata6mo ago
https://gist.github.com/Namrata199/b913be06177c0b1317d334180e3826ce this is the updated gist with my latest code
Gist
Filament Page Data not changing
Filament Page Data not changing. GitHub Gist: instantly share code, notes, and snippets.
LeandroFerreira
LeandroFerreira6mo ago
try private $apiData = null;
namrata
namrata6mo ago
This worked !! thanks a lot !! Really appreciate this !!