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
tesse059mo ago
top
StefanD
StefanD8mo 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.8mo 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
StefanD8mo 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.8mo ago
just use the DB man don't think it needs to be that diffacult, DB::SELECT()->GROUPBY() etc.....
StefanD
StefanD8mo ago
In my case, it is a difficult Statement, but I just found out, that I need to use "modifyBaseQueryUsing".
Solution
StefanD
StefanD8mo ago
This function is doing the trick.