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