Skip to content

Commit

Permalink
fix support real mapp
Browse files Browse the repository at this point in the history
  • Loading branch information
xxl4 committed Nov 20, 2024
1 parent 2abe2ae commit e3465b9
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Http/Controllers/Api/V1/Admin/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,55 @@ class ResourceController extends V1Controller implements ResourceContract
public function allResources(Request $request)
{
$query = $this->getRepositoryInstance()->scopeQuery(function ($query) use ($request) {
foreach ($request->except($this->requestException) as $input => $value) {
$query = $query->whereIn($input, array_map('trim', explode(',', $value)));

$filterable = $request->except($this->requestException);

//var_dump($request->all());exit;

foreach ($filterable as $input => $value) {
$relations = explode('-', $input);
$fieldWithOperator = array_pop($relations);

if (strpos($fieldWithOperator, '__') !== false) {
[$field, $operator] = explode('__', $fieldWithOperator);
} else {
$field = $fieldWithOperator;
$operator = 'eq';
}

$operatorMap = [
'eq' => '=',
'neq' => '<>',
'gt' => '>',
'lt' => '<',
'gte' => '>=',
'lte' => '<=',
'like' => 'like',
];

$operator = $operatorMap[$operator] ?? '=';

if (!empty($relations)) {
$relation = implode('.', $relations);

$query = $query->whereHas($relation, function ($q) use ($field, $operator, $value) {
if ($operator === 'like') {
$value = "%{$value}%";
}
$q->where($field, $operator, $value);
});
} else {
if ($operator === 'like') {
$value = "%{$value}%";
}
$query = $query->where($field, $operator, $value);
}
}

// foreach ($request->except($this->requestException) as $input => $value) {
// $query = $query->whereIn($input, array_map('trim', explode(',', $value)));
// }

if ($sort = $request->input('sort')) {
$query = $query->orderBy($sort, $request->input('order') ?? 'desc');
} else {
Expand Down

0 comments on commit e3465b9

Please sign in to comment.