© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
FilamentF
Filament•14mo ago
Pedroesca

Custom search in table

I have a little situation here:
When I get ONLY ONE record, I need to add it to the cart (that happens, I just have to refresh the page with F5 for it to be reflected);
When I search by other criteria, it gets them correctly but when I press the action row, IT ALWAYS ADDS THE FIRST record in the table.
I know I'm doing something wrong.
public function table(Table $table): Table
    {
        return $table
            ->query(function()
                {   
                    if ($this->tableSearch === '') {
                        return Product::query()->whereRaw('1 = 0');
                    } else {
                        $product = Product::get()
                            ->where('barcode', '=',  $this->tableSearch);
                        if ($product->count() === 1) {
                            $this->addToCart($product->first()->id);
                            $this->updateCart();
                            $this->resetTableSearch();
                            return Product::query()->whereRaw('1 = 0');
                        } else {
                            return Product::query()
                                ->where('name', 'like', '%' . $this->tableSearch . '%')
                                ->orWhere('barcode', 'like', '%' . $this->tableSearch . '%');
                        }
                    }
                    return Product::query();
                })
            ->columns([
                TextColumn::make('name')->searchable(),
                TextColumn::make('barcode')->searchable(),
            ])
            ->actions([
                Action::make('add-cart')
                    ->button()
                    ->action(function($record){
                        $this->addToCart($record->id);
                        //dd($record);
                    })
            ]);
    }
public function table(Table $table): Table
    {
        return $table
            ->query(function()
                {   
                    if ($this->tableSearch === '') {
                        return Product::query()->whereRaw('1 = 0');
                    } else {
                        $product = Product::get()
                            ->where('barcode', '=',  $this->tableSearch);
                        if ($product->count() === 1) {
                            $this->addToCart($product->first()->id);
                            $this->updateCart();
                            $this->resetTableSearch();
                            return Product::query()->whereRaw('1 = 0');
                        } else {
                            return Product::query()
                                ->where('name', 'like', '%' . $this->tableSearch . '%')
                                ->orWhere('barcode', 'like', '%' . $this->tableSearch . '%');
                        }
                    }
                    return Product::query();
                })
            ->columns([
                TextColumn::make('name')->searchable(),
                TextColumn::make('barcode')->searchable(),
            ])
            ->actions([
                Action::make('add-cart')
                    ->button()
                    ->action(function($record){
                        $this->addToCart($record->id);
                        //dd($record);
                    })
            ]);
    }
Filament banner
FilamentJoin
A powerful open source UI framework for Laravel • Build and ship admin panels & apps fast with Livewire
20,307Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Custom Action within TablesRenderHook::TOOLBAR_SEARCH_BEFORE
FilamentFFilament / ❓┊help
12mo ago
Add custom action beside search table
FilamentFFilament / ❓┊help
3y ago
Get search value in table to add custom query to table query
FilamentFFilament / ❓┊help
3y ago
search id column in table
FilamentFFilament / ❓┊help
3y ago