ConfigController.php 1.66 KB
<?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))
        };
    }
}