F
Filament2mo ago
Nick

How-to create table filter from model accessor

Hello, I have an app that uses Laravel batches and I have a field on my "batched" model to track the batch and provide a status based on Laravel's built-in batch methods:
protected function batchStatus(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes) {
$batch = $this->getBatch(); //All this does is check for a batch ID and if it is present, retrieve the batch from Laravel's Bus as defined in the docs

if (!$batch) {
return 'No Batch Found';
} else {
if($batch->finished()) { return 'Finished'; }
elseif($batch->cancelled()) { return 'Cancelled'; }
elseif($this->inProgress) { return 'In Progress'; }
elseif (!$batch->finished() && $batch->failedJobs > 0) { return 'Failed'; }
elseif (!$batch->finished() && $batch->pendingJobs > 0) { return 'Waiting'; }
else { return 'Unknown Status'; }
}
},
);
}
protected function batchStatus(): Attribute
{
return Attribute::make(
get: function (mixed $value, array $attributes) {
$batch = $this->getBatch(); //All this does is check for a batch ID and if it is present, retrieve the batch from Laravel's Bus as defined in the docs

if (!$batch) {
return 'No Batch Found';
} else {
if($batch->finished()) { return 'Finished'; }
elseif($batch->cancelled()) { return 'Cancelled'; }
elseif($this->inProgress) { return 'In Progress'; }
elseif (!$batch->finished() && $batch->failedJobs > 0) { return 'Failed'; }
elseif (!$batch->finished() && $batch->pendingJobs > 0) { return 'Waiting'; }
else { return 'Unknown Status'; }
}
},
);
}
I have this attribute as a field in my table and I want to be able to filter on it with a Select filter, but since it isn't a DB field, I can't figure out how to setup the filter. Any help would be greatly appreciated!
0 Replies
No replies yetBe the first to reply to this messageJoin