Welcome Guest, Not a member yet? Register   Sign In
CI4 with join and pagination
#5

(This post was last modified: 08-21-2021, 07:06 PM by spreaderman. Edit Reason: Mistakenly thought was code was still incorrect, however, it was correct and the code works. )

Thank you for the code snippet. I realized I was not making a model object properly. I read a bit more and found this link;  https://stackoverflow.com/questions/6527...wing-error  It says pagination only works on model objects (far above I was creating a builder object).  I have revised my model and controller per below.  It is now working. Thank you.

Code:
    function index(){
        $this->data['posts'] = $this->PostModel->paginatePosts();
        $this->data['pager']    = $this->PostModel->pager;
    return view('Post/post_index', $this->data);
    }


and this is my new method in the model;

Code:
    function paginatePosts($post_category_id = null, $category_is_published = 1, $post_is_published = 1, $articles_per_page =6 ) {
       
        return $this->select('
            post.post_id,
            post.post_category_id,
            post.post_title,
            post.post_slug,
            post.post_body,
            post.post_is_published,
            post.post_updated_at,
            post.post_created_at,
            post.post_user_id,
            post.post_image,
            user.name,
            user.email,
            category.category_id,
            category.category_user_id,
            category.category_name,
            category.category_is_published,
            category.category_created_at,
            category.category_updated_at')
            ->join('category', 'category.category_id = post.post_category_id')
            ->join('user', 'user.id = post.post_user_id')
            ->orderBy('post.post_updated_at', 'DESC')
            ->where('post.post_is_published', $post_is_published)
            ->where('category.category_is_published', $category_is_published)
            ->where('post.post_is_published', $post_is_published)
            ->where('category.category_is_published', $category_is_published)
            ->paginate($articles_per_page, 'bootstrap');
    }

and there is my call to echo links to the view;

Code:
<?= $pager->links('bootstrap', 'bootstrap4_pagination'); ?>
Reply


Messages In This Thread
CI4 with join and pagination - by spreaderman - 08-20-2021, 06:43 PM
RE: CI4 with join and pagination - by InsiteFX - 08-21-2021, 12:52 AM
RE: CI4 with join and pagination - by spreaderman - 08-21-2021, 01:20 AM
RE: CI4 with join and pagination - by paliz - 08-21-2021, 01:23 PM
RE: CI4 with join and pagination - by spreaderman - 08-21-2021, 06:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB