Gabotronix's avatar

Custom/Extend laravel pagination method

Hi everybody, 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 posible to chain or override laravel paginate method? 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();

0 likes
1 reply

Please or to participate in this conversation.