Custom Pagination for Nova Framework

David Carr

Nova Framework

Pagination that comes out the box with Nova Framework is great and very easy to use, for instance it can be as simple as:

DB::table('posts')->paginate();

The default pagination HTML output and styling works well, there may be times however when you want to change the style and HTML markup. This can be done by using your own pagination presenter class that extends from the Pagination Presenter. 

Lets build a Presenter that removes the li tags from around the page links and the disabled class. 

First create a new class the location is not too important it can be placed in anywhere in reality as long as it’s namespaced. I’m going to create a file called CustomPresenter.php located in app/Helper.

The class will need to import Pagination\Presenter and extend the Presenter. Then to override any methods simply create a method with the same name. I have methods getPrevious() and getNext() that return straight away, I do this as i do not want to have previous and next arrows as it does by default.

<?php
namespace App\Helpers;

use Pagination\Presenter;

class CustomPresenter extends Presenter
{
    /**
     * Get HTML wrapper for a page link.
     *
     * @param  string  $url
     * @param  int  $page
     * @param  string  $rel
     * @return string
     */
    public function getPageLinkWrapper($url, $page, $rel = null)
    {
        $rel = is_null($rel) ? '' : ' class="'.$rel.'"';

        return '<a href="'.$url.'"'.$rel.' class="page-numbers bg-border-color">'.$page.'</a>';
    }

    /**
     * Get HTML wrapper for disabled text.
     *
     * @param  string  $text
     * @return string
     */
    public function getDisabledTextWrapper($text)
    {
        return '<a href="">'.$text.'</a>';
    }

    /**
     * Get HTML wrapper for active text.
     *
     * @param  string  $text
     * @return string
     */
    public function getActivePageWrapper($text)
    {
        return '<a href="" class="page-numbers bg-border-color current">'.$text.'</a>';
    }

    /**
     * Get HTML wrapper for the entire paginator.
     *
     * @param  string  $content
     * @return string
     */
    public function getPaginationWrapper($content)
    {
        return '<nav class="navigation align-center">' .$content .'</nav>';
    }

    public function getPrevious($text = '&laquo;'){
        return;
    }

    public function getNext($text = '&raquo;'){
        return;
    }

}

Now when you want to use this custom presenter first import it:

use App\Helpers\CustomPresenter;

Next use the custom presenter by creating a new instance of the class and setting it as the presenter:

//paginate records
$posts = Post::paginate();

//setup custom presenter
$presenter = new CustomPresenter($posts);

//set the presenter to be used on the paginated object ie $posts pass in the $presenter
$posts->setPresenter($presenter);

That’s it from there the usage is the same as always to display the links:

echo $posts->links();

 

Fathom Analytics $10 discount on your first invoice using this link

Help support the blog so that I can continue creating new content!

Sponsor

Fathom Analytics $10 discount on your first invoice using this link

Subscribe to my newsletter

Subscribe and get my books and product announcements.

© 2006 - 2024 DC Blog. All code MIT license. All rights reserved.