Skip to content

Commit

Permalink
Fix the Multiselect query scope to let multiple fields using it
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Nov 10, 2024
1 parent 6aef554 commit f22f015
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/Scopes/Multiselect.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ class Multiselect extends Scope
*/
public function apply($query, $values)
{
$prefix = $values->get('query_scope').':';

$filters = collect($values)->filter(function ($value, $key) use ($prefix) {
return Str::startsWith($key, $prefix);
})->mapWithKeys(function ($value, $key) use ($prefix) {
$newKey = Str::after($key, $prefix);
collect($values)->filter(function ($value, $key) {
return Str::startsWith($key, 'multiselect:');
})->mapWithKeys(function ($value, $key) {
$newKey = Str::after($key, 'multiselect:');

return [$newKey => explode('|', $value)];
})->all();

$field = array_key_first($filters);
})->each(function ($values, $field) use ($query) {
$query->where(function ($query) use ($field, $values) {
foreach ($values as $value) {
$query->orWhereJsonContains($field, $value);
}
});
});

return $query->whereJsonContains($field, $filters[$field]);
return $query;
}
}

0 comments on commit f22f015

Please sign in to comment.