Welcome Guest, Not a member yet? Register   Sign In
Pagination
#1

Hello everyone, I am new to Codeigniter 4... And I'am trying to figure this pagination out...

I've got this function, and it works as it should:
PHP Code:
public function butikk()
    {
        $model = new ButikkModel();

        $data = [
        'ovner' => $model->paginate(6),
        'pager' => $model->pager
      
];

        echo view('templates/v2/header');
        echo view('pages/butikk'$data);
        echo view('templates/v2/footer');
    

But this grabs everything from my table. I've different types of content in that table, differences with 1,2 or 3 as value in a column named type.

My model:
PHP Code:
public function getOvner($id false$type 1)
    {
        if ($slug === false) {
            $query $this->db->query("SELECT * FROM ovner WHERE type='$type' ORDER BY created_at DESC");
            $results $query->getResultArray();

            return $results;
        }

        return $this->asArray()
                ->where(['id' => $id])
                ->first();
    
Here I am running my query, and this works just fine without the pagination part.

So what I discovered was, when using "->pagination" in my controller, it totally ignores what I've got in my model.
This is obviously because it never runs this function. 

How can I implement this? So it only grabs lets say type 1.

Thanks in advance!

Regards Stian.
Reply
#2

The getOvner method returns the result. For pagination to work based on your request, you only need to prepare the request, not return it. Which implies using only QueryBuilder.

PHP Code:
// model method 
public function myMethod()
{
    
$this->builder()->where('field''value');
    return 
$this;  
}

// Example to call
$model = new MyModel();

$model->myMethod()->paginate(1); 
Reply
#3

(03-08-2021, 12:29 AM)iRedds Wrote: The getOvner method returns the result. For pagination to work based on your request, you only need to prepare the request, not return it. Which implies using only QueryBuilder.

PHP Code:
// model method 
public function myMethod()
{
    $this->builder()->where('field''value');
    return $this;  
}

// Example to call
$model = new MyModel();

$model->myMethod()->paginate(1); 

Thank you very much! Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB