Here's a one-liner solution to transform the query parameters to an Eloquent where query:
$result = collect($request->query())
->only(array_keys($allowedParams))
->flatMap(function ($queryParam, $column) use ($columnMap) {
return collect($queryParam)
->only($allowedParams[$column])
->map(function ($value, $operator) use ($columnMap, $column) {
return [$columnMap[$column], QueryOperators::from($operator)->value(), $value];
});
})
->values()
->toArray();
This code does the following:
- Collects the query parameters from the request and keeps only the allowed ones.
- Maps each query parameter to an array of Eloquent where clauses.
- Flattens the resulting array of arrays into a single array.
- Removes any empty arrays from the result.
- Returns the result as an array.
Note that this code assumes that the QueryOperators class is defined and that it provides a from method that returns an instance of the appropriate query operator based on the given operator string (e.g. "eq" => "=").