F
Filament13mo ago
tesse05

Filter queries not override the eloquent query

Hi there! I have a FIlter in my Table Builder, in which I would like to filter by a computed property setted in resource main page, eloquent query method. I have check that the query works fine at SQL editor. When I apply the ->query() method from filter, it overrides the eloquent query and the field is not found. How to manage it??
Filter::make('available_slots')
->form([
TextInput::make('qtat')
->label('Aforament mínim')
->numeric()
])
->query(function (
Builder $query,
array $data
): Builder {
// it overrides $livewire->getTableQuery()
return $query
->when(
$data['qtat'],
function (
Builder $query,
$availableSlots,
): Builder {
return $query->having('available_slots',
'>=', $availableSlots);
});
})
Filter::make('available_slots')
->form([
TextInput::make('qtat')
->label('Aforament mínim')
->numeric()
])
->query(function (
Builder $query,
array $data
): Builder {
// it overrides $livewire->getTableQuery()
return $query
->when(
$data['qtat'],
function (
Builder $query,
$availableSlots,
): Builder {
return $query->having('available_slots',
'>=', $availableSlots);
});
})
Solution:
This function is doing the trick.
Jump to solution
7 Replies
tesse05
tesse05OP13mo ago
top
StefanD
StefanD12mo ago
Have you found a solution. I need to add selects and order by states to the query. Is is also not working.
Quin.
Quin.12mo ago
Filter::make('available_slots')
->form([
TextInput::make('qtat')
->label('Aforament mínim')
->numeric()
])
->query(function (Builder $query, array $data): Builder {
return $query->when(
$data['qtat'],
function (Builder $query, $availableSlots): Builder {
return $query->where(function ($query) use ($availableSlots) {
// Add your computed property logic here
$query->whereRaw('yourproperty >= ?', [$availableSlots]);
});
}
);
});
Filter::make('available_slots')
->form([
TextInput::make('qtat')
->label('Aforament mínim')
->numeric()
])
->query(function (Builder $query, array $data): Builder {
return $query->when(
$data['qtat'],
function (Builder $query, $availableSlots): Builder {
return $query->where(function ($query) use ($availableSlots) {
// Add your computed property logic here
$query->whereRaw('yourproperty >= ?', [$availableSlots]);
});
}
);
});
Replace yourproperty with your own property
StefanD
StefanD12mo ago
Hi Quin, thank you for your prompt answer. But 'When' and 'Where' statements are not the problem. I need to add selects and group by statements to the query. But thy are not added to the main query.
Quin.
Quin.12mo ago
just use the DB man don't think it needs to be that diffacult, DB::SELECT()->GROUPBY() etc.....
StefanD
StefanD12mo ago
In my case, it is a difficult Statement, but I just found out, that I need to use "modifyBaseQueryUsing".
Solution
StefanD
StefanD12mo ago
This function is doing the trick.
Want results from more Discord servers?
Add your server