Combination of two filters #967
Replies: 1 comment
-
I was looking for something like this. The situation in our application is slightly different. There are several defined filters the user can choose from, and some of them have a couple of other options for filtering on their own. These other options by themselves don't make sense so if the "main" filter is not set they do nothing Your "empty" callbacks provided me the inspiration to solve it and this is what I came up with $rows = QueryBuilder::for(File::class)
->allowedFilters([
AllowedFilter::callback('sent-from', fn(Builder $builder, $value) => $builder),
AllowedFilter::callback('sent-to', fn(Builder $builder, $value) => $builder),
AllowedFilter::callback('sent', function(Builder $builder, $value, $property) use ($request){
$filterKey = config('query-builder.parameters.filter', 'filter');
$from = $request->get($filterKey)['sent-from'] ?? null;
$to = $request->get($filterKey)['sent-to'] ?? null;
$builder->sentFilter($value, $from, $to);
}),
])
->defaultSort('-updated_at')
->paginate($this->perPage()); I delegate to a scope inside the callback so the controller it's a bit cleaner. It's a bit hacky but I don't think it looks that terrible. |
Beta Was this translation helpful? Give feedback.
-
Hi!
I've been encoutering a use case on a project that seem not to be covered by the package. Indeed, we're using Laravel Query Builder since it is a great package and it increases the readibility of our query endpoints by far.
However, I'm writing a query where a filter actually depends on 2 filters. I want to filter the results by location: city and regions lets say. If I check city A, B and regions A, I would like to have results in cities A and B and also results in region A.
It's like a
Thus the query string is looking like
I just have no idea whether it could be handled by the package or not, and if it could, how would it looks like?
The workaround I have right now is to add 2 callback filters (so it allows the filters) but actually move the logic in a
when
method... As said, it looks like a workaround, and as it does not look like an issue, I thought it could be discussed.Unless there's a way to handle it properly right now, I thought it could look like something like this:
Beta Was this translation helpful? Give feedback.
All reactions