ConfigController.php
1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace App\Http\Container\ProviderSection\Controllers;
use App\Models\SystemConfig;
use Arr;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiController;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
class ConfigController extends ApiController
{
/**
* @param \Illuminate\Http\Request $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function __invoke(Request $request): Response
{
$pageSize = $request->get('pageSize', 20);
$identifier = Arr::wrap($request->get('identifier', []));
$parentKey = Arr::wrap($request->get('parentKey', []));
if (count($parentKey) !== 0) {
$identifier = SystemConfig::query()->whereIn('identifier', $parentKey)->where('status', 1)->pluck('identifier')->merge($identifier)->toArray();
}
SystemConfig::resolveRelationUsing('children', static function (SystemConfig $config) {
return $config->hasMany(SystemConfig::class, 'parent_id')->latest('weight')->with('children:id,identifier,name,content,parent_id');
});
$build = SystemConfig::query()
->when($request->integer('hasChildren') === 1, fn(Builder $query) => $query->with('children:id,identifier,name,content,parent_id'))
->select(['id', 'identifier', 'name', 'content'])
->whereIn('identifier', $identifier)
->where('status', 1)
->latest('weight');
return match ($request->get('fetchType', 'page')) {
'all' => $this->successWithData($build->get()),
default => $this->successWithData($build->paginate($pageSize))
};
}
}