middleware( function ($request, $next) { $this->repository = app(CurrencyRepositoryInterface::class); // new way of user group validation $userGroup = $this->validateUserGroup($request); $this->repository->setUserGroup($userGroup); return $next($request); } ); } public function index(IndexRequest $request): JsonResponse { $settings = $request->getAll(); if (true === $settings['enabled']) { $currencies = $this->repository->get(); } if (true !== $settings['enabled']) { $currencies = $this->repository->getAll(); } $pageSize = $this->parameters->get('limit'); $count = $currencies->count(); // depending on the sort parameters, this list must not be split, because the // order is calculated in the account transformer and by that time it's too late. $accounts = $currencies->slice(($this->parameters->get('page') - 1) * $pageSize, $pageSize); $paginator = new LengthAwarePaginator($accounts, $count, $pageSize, $this->parameters->get('page')); $transformer = new CurrencyTransformer(); $this->parameters->set('pageSize', $pageSize); $transformer->setParameters($this->parameters); // give params to transformer return response() ->json($this->jsonApiList(self::RESOURCE_KEY, $paginator, $transformer)) ->header('Content-Type', self::CONTENT_TYPE) ; } }