Welcome Guest, Not a member yet? Register   Sign In
How to create pagination on CI4
#15

(This post was last modified: 09-18-2021, 01:07 AM by InsiteFX.)

This is how I do it for my Blog I'm building.

PHP Code:
// post model method

/**
 * getLivePosts ()
 * -------------------------------------------------------------------
 *
 */
public function getLivePosts() : PostModel
{
    $builder $this->builder();

    $builder->where('status''published')
            ->orderBy('created_at''desc');
        
    
// for method chaining
    return $this;
}

// Blog Controller

/**
 * -------------------------------------------------------------------
 * posts ()
 * -------------------------------------------------------------------
 *
 */
public function posts()
{
    $pager Services::pager();

    $posts      = new PostModel($this->request);
    $categories = new CategoryModel();

    $data = [
        "featured"    => $posts->getFeaturedPost(),
        'posts'      => $posts->getLivePosts()->paginate(4'group1'),
        'pager'      => $posts->pager,
        'currentPage' => $posts->pager->getCurrentPage('group1'),
        'totalPages'  => $posts->pager->getPageCount('group1'),
        'categories'  => $categories->getTopCategories(),
        'title'      => 'Blog Home',
        'pageHeading' => 'Welcome to our Blog!',
        'subHeading'  => '',
        'typography'  => Services::typography(),
    ];

    echo view('Insitefx\Blog\Views\posts\index');
}

Notic the model returning $this for method chaining
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
How to create pagination on CI4 - by Marcolino92 - 04-08-2020, 10:15 AM
RE: How to create pagination on CI4 - by wdeda - 04-09-2020, 08:38 AM
RE: How to create pagination on CI4 - by wdeda - 04-09-2020, 10:44 AM
RE: How to create pagination on CI4 - by wdeda - 04-09-2020, 01:01 PM
RE: How to create pagination on CI4 - by waleed - 02-18-2021, 03:08 AM
RE: How to create pagination on CI4 - by Mano - 09-17-2021, 05:29 PM
RE: How to create pagination on CI4 - by wdeda - 04-10-2020, 05:09 PM
RE: How to create pagination on CI4 - by InsiteFX - 09-18-2021, 01:07 AM
RE: How to create pagination on CI4 - by danger89 - 02-26-2024, 05:53 PM



Theme © iAndrew 2016 - Forum software by © MyBB