F
Filament12mo ago
namrata

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
LeandroFerreira12mo ago
Livewire
Lifecycle Hooks | Livewire
A full-stack framework for Laravel that takes the pain out of building dynamic UIs.
namrata
namrataOP12mo ago
I used mount but it did not work. The problem is the data doesnot change even when I apply the date filter
namrata
namrataOP12mo ago
LeandroFerreira
LeandroFerreira12mo ago
->modifyQueryUsing(fn () => $this->apiData)
->modifyQueryUsing(fn () => $this->apiData)
?
namrata
namrataOP12mo 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
namrataOP12mo ago
No description
LeandroFerreira
LeandroFerreira12mo ago
are you using Filament v3?
namrata
namrataOP12mo ago
yes
Solution
LeandroFerreira
LeandroFerreira12mo 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
namrataOP12mo 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
namrataOP12mo ago
so there is no way ti get the data I want ? with my query ?
LeandroFerreira
LeandroFerreira12mo ago
If it returns an Eloquent Builder, it will work
namrata
namrataOP12mo 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
LeandroFerreira12mo ago
try product_id as id in the select
namrata
namrataOP12mo ago
gives this
No description
LeandroFerreira
LeandroFerreira12mo ago
share the whole code
namrata
namrataOP12mo 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
LeandroFerreira12mo ago
try to share the whole code in gist please
namrata
namrataOP12mo 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
LeandroFerreira12mo ago
try private $apiData = null;
namrata
namrataOP12mo ago
This worked !! thanks a lot !! Really appreciate this !!
Want results from more Discord servers?
Add your server