/** * Get all comments of a user, paginated * * @param $user_id * * @param Request $request * * @return array */ public function userComments($user_id, Request $request) { // get limit $limit = $request->get('limit') ?: 5; $limit = $limit > 5 ? 5 : $limit; try { // Get user $user = User::select('user_id', 'username', 'created_at')->findOrFail($user_id); // Get comments of user, paginated $comments = $user->comments()->select('comment_id', 'comment', 'created_at')->paginate($limit); } catch (ModelNotFoundException $e) { return ResponseHelper::write(404, 1001, 'The requested user does not exist.'); } // Get transformer for user and user comments $userTransformer = new UserTransformer(); $commentTransformer = new UserCommentTransformer(); // Return transformed user and comments return ResponseHelper::withPaginationData(['user' => $userTransformer->transform($user->toArray()), 'comments' => $commentTransformer->transformCollection($comments->toArray()['data'])], $comments); }