Welcome Guest, Not a member yet? Register   Sign In
Using the built-in pagination functions
#7

All queries should be placed in your model.
My Pagination uses my Bootstrap 5 Pagination Template.

Model:
PHP Code:
/**
 * getLivePosts ()
 * -------------------------------------------------------------------
 *
 * @return PostModel
 */
 
public function getLivePosts() : PostModel
 
{
 
$builder $this->builder();

 
$builder->where('status''published')
 
        ->orderBy('created_at''desc');

        // for method chaining
 
return $this;
 } 

Controller:
PHP Code:
/**
 * -------------------------------------------------------------------
 * posts ()
 * -------------------------------------------------------------------
 *
 * Get all Blog live posts
 * 
 */
public function posts()
{
    $pager Services::pager();

    $posts      = new PostModel();
    $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(),
    ];

    $data $this->mergeGlobalData($data);

    echo view('Insitefx\Blog\Views\posts\index_posts'$data);

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply


Messages In This Thread
RE: Using the built-in pagination functions - by InsiteFX - 01-21-2022, 01:51 AM



Theme © iAndrew 2016 - Forum software by © MyBB