Use Filament Table Filters With Sushi
Hello, I created a simple resource and my table has these filters.
->filters([
Filter::make('Period')
->form([
Flatpickr::make('from')
->default(Carbon::now()->format('Y-m'))
->monthSelect()
->beforeOrEqual('to')
->maxDate(now()->firstOfMonth())
->required(true),
Flatpickr::make('to')
->default(Carbon::now()->format('Y-m'))
->monthSelect()
->afterOrEqual('from')
->maxDate(now()->lastOfMonth())
->required(),
])
->default()
->columns(2)
In the Manage Class I have overridden the method getTableQuery() to set from and to variable in the Model and use them in the Sushi getRows() method.
protected function getTableQuery(): ?Builder
{
if(isset($this->tableFilters['Period']['from']) && isset($this->tableFilters['Period']['to'])){
ModelName::setPeriod($this->tableFilters['Period']['from'],$this->tableFilters['Period']['to']);
}
return ModelName::query();
}
This is my model
class Name extends Model
{
use \Sushi\Sushi;
protected static $from;
protected static $to;
public function getRows()
{
info(self::$from);
info(self::$to);
// The query
}
public static function setPeriod($from, $to): Builder
{
self::$from = $from;
self::$to = $to;
return self::query();
}
protected function sushiShouldCache()
{
return false;
}
}
When I set the from and to dates using the filter, data is filtered using the previous value of these filters instead of the current one.
Any suggestions?0 Replies