Table SelectFilter

I know I am missing something simple here, so just need a second set of eyes.
SelectFilter::make('status_id')
->label('Status')
->relationship('status', 'name'),
SelectFilter::make('type')
->label('Status Type')
->relationship('status', 'type'),
SelectFilter::make('status_id')
->label('Status')
->relationship('status', 'name'),
SelectFilter::make('type')
->label('Status Type')
->relationship('status', 'type'),
The type is just open or closed. Right now it doesnt filter by it and for some reason it lists 10 options in the select with half being open and half closed. Filtering by status name works fine.
Solution:
I ended up doing ```php SelectFilter::make('type') ->label('Status Type') ->options([ 'open' => 'Open',...
Jump to solution
4 Replies
Solution
Mark Chaney
Mark Chaney14mo ago
I ended up doing
SelectFilter::make('type')
->label('Status Type')
->options([
'open' => 'Open',
'closed' => 'Closed',
])
->default(function () {
if (request()->routeIs('outreach-activities')) {
return 'open';
}
})
->query(
function ($query, $data) {
$selectedStatus = $data['value'] ?? null;

$query->when($selectedStatus, function ($q) use ($selectedStatus) {
$q->whereHas('status', function ($subQuery) use ($selectedStatus) {
$subQuery->where('type', $selectedStatus);
});
});
}
),
SelectFilter::make('type')
->label('Status Type')
->options([
'open' => 'Open',
'closed' => 'Closed',
])
->default(function () {
if (request()->routeIs('outreach-activities')) {
return 'open';
}
})
->query(
function ($query, $data) {
$selectedStatus = $data['value'] ?? null;

$query->when($selectedStatus, function ($q) use ($selectedStatus) {
$q->whereHas('status', function ($subQuery) use ($selectedStatus) {
$subQuery->where('type', $selectedStatus);
});
});
}
),
Dennis Koch
Dennis Koch14mo ago
I think you figured out, that the original code didn't work, because it's still filtering by ID and type is only for display
Mark Chaney
Mark ChaneyOP14mo ago
so would there even be a way to do it using the relationship method?
Dennis Koch
Dennis Koch14mo ago
I don't think so. I mean you could use relationship() for the options, but need to overwrite the query
Want results from more Discord servers?
Add your server