middleware( function ($request, $next) { $this->repository = app(AccountRepositoryInterface::class); // new way of user group validation $userGroup = $this->validateUserGroup($request); if (null !== $userGroup) { $this->repository->setUserGroup($userGroup); } return $next($request); } ); } public function infiniteList(InfiniteListRequest $request): JsonResponse { $this->repository->resetAccountOrder(); // get accounts of the specified type, and return. $types = $request->getAccountTypes(); // get from repository $accounts = $this->repository->getAccountsInOrder($types, $request->getSortInstructions('accounts'), $request->getStartRow(), $request->getEndRow()); $total = $this->repository->countAccounts($types); $count = $request->getEndRow() - $request->getStartRow(); $paginator = new LengthAwarePaginator($accounts, $total, $count, $this->parameters->get('page')); $transformer = new AccountTransformer(); $transformer->setParameters($this->parameters); // give params to transformer return response() ->json($this->jsonApiList(self::RESOURCE_KEY, $paginator, $transformer)) ->header('Content-Type', self::CONTENT_TYPE) ; } }