Custom/Extend laravel pagination method

Discussion in 'PHP' started by Blmz, Apr 1, 2021.

  1. #1
    I use vue component for displaying data on my blade template layouts, like for example pagination data, that's why I created a method to return paginated data in the shape I need it, I was wondering if it's possible to chain or override laravel paginate method? Found an article that helped me, but I can't put my finger on it.
    https://adevait.com/laravel/laravel-overwriting-default-pagination-system
    Right now I'm doing it like this but I'd like to maybe extend laravel query builder or whatever classes laravel uses to paginate:

    $posts = Post::latest()->with(['category','user'])->paginate($request->input('paginate', 6));

    $posts = $this->getPagination($posts);

    public function getPagination($results)
    {
    return
    [
    'data' => $results,
    'pagination' => [
    'total' => $results->total(),
    'per_page' =>$results->perPage(),
    'current_page' => $results->currentPage(),
    'last_page' => $results->lastPage(),
    'from' => $results->firstItem(),
    'to' => $results->lastItem()
    ]
    ];
    }

    I'd like to have something like:

    $posts = Post::paginate($request->input('paginate', 6))->getPaginatedData();
     
    Blmz, Apr 1, 2021 IP