Allow a way to disable Array Delimiters #914
Unanswered
Spice-King
asked this question in
Ideas
Replies: 1 comment
-
I also encountered this on a few apps in production. Perhaps the documentation on Callback filters should be updated with a reminder stating that In the meantime, this is how I've dealt with it. I created a wrapper to handle multiple values and invoke the callback across all values using use Spatie\QueryBuilder\AllowedFilter;
class AllowedFilterCallback
{
public static function make($name, callable $callback)
{
return AllowedFilter::callback($name, function ($query, $value) use ($callback) {
// Value can sometimes be an array, when search term contains commas.
// Query Builder package treats this as multiple-value search.
return $query->where(function ($query) use ($value, $callback) {
$values = collect($value)->filter();
// We will auto-wrap multi-values into separate OR clauses.
foreach ($values as $searchTerm) {
$query->orWhere(function ($q) use ($searchTerm, $callback) {
return $callback($q, $searchTerm);
});
}
});
});
}
} And then wherever |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Bumped into an issue in production where someone dumped a comma into a search box, ended up throwing an exception as it was a callback filter that had typed parameters setting the value to be a string and not an array.
Few options come to mind, like setting it to
false
or an empty string (which does work right now, but I consider it to be a byproduct of happenstance that ``Str::contains` always returns false for an empty string as a search, thus not exploding the string, and not a proper documented feature) to disable it.Another option is to provide a way to escape the delimiter so that the issue can be avoided. This kinda feels like a bandaid more than a fix.
The last option is to look at the passed in closure to see if it expects a single value (string, int, float, boolean, etc) or an array, then exploding if it's an array. This would at least be consistent with for times that there might be either a single or multiple values being both arrays for the callback.
I'm just going to abuse the empty string option for now, but it feels more jank than I'd expect for something in Laravel..
Beta Was this translation helpful? Give feedback.
All reactions