#Filament page filter

26 messages · Page 1 of 1 (latest)

hazy burrow
thin hareBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

dire trout
hazy burrow
#

I used mount but it did not work. The problem is the data doesnot change even when I apply the date filter

dire trout
hazy burrow
#

where do i do this ?

#
  protected function getTableQuery(): Builder
    {
        return Product::where('active', true)->publicDisplay()
            ->orderBy('orders_count', 'desc')->modifyQueryUsing(fn () => $this->apiData);
    }

this gives error

dire trout
hazy burrow
#

yes

dire trout
hazy burrow
#

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,
            ];
        });
    }

here the table part crashes.

hazy burrow
#

so there is no way ti get the data I want ? with my query ?

dire trout
hazy burrow
#

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;
    }
dire trout
hazy burrow
#

gives this

dire trout
hazy burrow
#
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;
    }
dire trout
hazy burrow
hazy burrow
#

This worked !! thanks a lot !! Really appreciate this !!

thin hareBOT