Use Enums in modifyQueryUsing for the ListRecord getTabs

I am using Enums in select on the form and in a selec filter but don't get how to use in a getTabs in the ListRecord page of my Resource. I was using another method and it worked fine but the enum is more sustainable. In this example the Enum 10 is labeled "Running"
return [
'all' => Tab::make('All'),
'Running' => Tab::make('Running')
->modifyQueryUsing(fn (Builder $query) => $query->where('status_id', 10))

// Not sure how to get the proper Enum here
//->modifyQueryUsing(fn (Builder $query) => $query->where('status_id', CampaignStatusEnum))
return [
'all' => Tab::make('All'),
'Running' => Tab::make('Running')
->modifyQueryUsing(fn (Builder $query) => $query->where('status_id', 10))

// Not sure how to get the proper Enum here
//->modifyQueryUsing(fn (Builder $query) => $query->where('status_id', CampaignStatusEnum))

namespace App\Enums;
use Filament\Support\Contracts\HasLabel;
enum CampaignStatusEnum: int implements HasLabel
{
case New = 0;
case Ready = 1;
case Run = 10;
case Paused = 30;
case Ended = 40;

public function getLabel(): ?string
{
return match ($this) {
self::New => 'New',
self::Ready => 'Ready To Launch',
self::Run => 'Running',
self::Paused => 'Paused',
self::Ended => 'Ended',
};
}
}
namespace App\Enums;
use Filament\Support\Contracts\HasLabel;
enum CampaignStatusEnum: int implements HasLabel
{
case New = 0;
case Ready = 1;
case Run = 10;
case Paused = 30;
case Ended = 40;

public function getLabel(): ?string
{
return match ($this) {
self::New => 'New',
self::Ready => 'Ready To Launch',
self::Run => 'Running',
self::Paused => 'Paused',
self::Ended => 'Ended',
};
}
}
the query should be ``` $query->where('status_id', CampaignStatusEnum::Run->value )
2 Replies
krekas
krekas5mo ago
status_id is a relationship column? cause it sounds like it this
ddoddsr
ddoddsrOP5mo ago
This is how I solved my problem:
'new' => Tab::make('New')
->modifyQueryUsing(fn (Builder $query) =>
$query->where('status', CampaignStatusEnum::New)),
'new' => Tab::make('New')
->modifyQueryUsing(fn (Builder $query) =>
$query->where('status', CampaignStatusEnum::New)),
Want results from more Discord servers?
Add your server