main
This commit is contained in:
Executable
+63
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace App\DB\Te;
|
||||
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Arr;
|
||||
use Symfony\Component\HttpFoundation\Exception\BadRequestException;
|
||||
|
||||
class QuerySelector
|
||||
{
|
||||
public static function run(Request $request)
|
||||
{
|
||||
|
||||
if (!$request->has('action')) {
|
||||
throw new BadRequestException();
|
||||
}
|
||||
$input = $request->all();
|
||||
if (!isset($input['query']) && !isset($input['default'])) {
|
||||
return response()->json(null);
|
||||
}
|
||||
$result = QuerySelector::searchQuery($request->all());
|
||||
return response()->json($result);
|
||||
}
|
||||
|
||||
public static function searchQuery($input)
|
||||
{
|
||||
|
||||
$conf = Arr::get(config('query'), $input['action'], null);
|
||||
// dd($conf);
|
||||
if (is_null($conf)) {
|
||||
return null;
|
||||
}
|
||||
$conf = (object)$conf;
|
||||
if (isset($conf->fn) && isset($input['query'])) {
|
||||
return call_user_func($conf->fn, $input['query'], $input['extra'] ?? null);
|
||||
}
|
||||
$cols = explode(',', $conf->cols);
|
||||
if (isset($input['query'])) {
|
||||
$query = $conf->model::where($cols[0], 'like', '%' . $input['query'] . '%');
|
||||
}
|
||||
|
||||
|
||||
if (isset($input['default'])) {
|
||||
if (is_array($input['default'])) {
|
||||
$query = $conf->model::whereIn('id', $input['default']);
|
||||
} else {
|
||||
$query = $conf->model::whereId($input['default']);
|
||||
}
|
||||
}
|
||||
if (isset($conf->where)) {
|
||||
foreach ($conf->where as $ww) {
|
||||
$query->where(...$ww);
|
||||
}
|
||||
}
|
||||
if (isset($conf->select)) {
|
||||
$select = (is_callable($conf->select)) ? call_user_func($conf->select) : $conf->select;
|
||||
return $query->select($select)->limit(20)->get()->toArray();
|
||||
} else {
|
||||
return $query->select(array_merge(['id'], $cols))->limit(20)->get()->toArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user