Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error returned when trying to initiate pagination in Codeigniter 4

I'm trying to initiate the pagination function in Codeigniter 4 as per the documentation.

$model = new \App\Models\UserModel();

$data = [
    'users' => $model->paginate(10),
    'pager' => $model->pager
];

My controller code is the following:

public function jobmarket() {
    $this->jobs = new \App\Models\Jobs();

    if (!$this->ionAuth->loggedIn())
    {
        return redirect()->to('/logg-inn');
    }

    echo view("dashboard/header", ([
        'ionAuth' => $this->ionAuth,
        'uri' => $this->uri,
    ]));

    echo view("dashboard/jobmarket", ([
        'session' => $this->session,
        'ionAuth' => $this->ionAuth,
        'validation' => $this->validator,
        'jobs' => $this->jobs->paginate(20)->all_jobs(),
        'pager' => $this->jobs->pager()->all_jobs(),
    ]));

    echo view("assets/footer");

}

However, when running this I get the following error:

Argument 1 passed to CodeIgniter\Database\BaseResult::getResult() must be of the type string, null given, called in xxxx/app/vendor/codeigniter4/framework/system/Model.php on line 447

This is my model

public function all_jobs() {
    $this->categories = new \App\Models\Categories();
    $builder = $this->db->table('jobs');
    $builder->select('*');
    $builder->join('users', 'users.id = jobs.jobs_u_id');
    $builder->join('categories', 'category_id = jobs.jobs_category');
    // Make sure to not show current user's listings, since these will show up under "My listings"
    $builder->where('jobs_u_id !=', $this->current_user->id);
    // Check that the listing reflects users chosen categories
    $builder->whereIn('category_id', $this->categories->user_categories());
    $builder->orderBy('jobs_id', 'desc');
    $query =  $builder->get();
    if ($builder->countAllResults() > 0)
    {
        return $query->getResult();

    } else {
        return false;
    }
}

Any help to resolve this would be appreciated.

like image 572
kanarifugl Avatar asked Oct 15 '22 04:10

kanarifugl


1 Answers

I don't know where you exactly get this error, but I found a few bugs in your code. Try to fix these bugs, maybe it helps you. Here are the bugs:

  1. The paginate() method returns the result, so it must be the last in your chain. Example: $this->jobs->all_jobs()->paginate(20)
  2. You can get a Pager like this: $this->jobs->pager
  3. If you want to use your all_jobs() method with the paginate() method, you have to return a model in your all_jobs() method

Here is the correct code for your controller:

public function jobmarket() {
    $this->jobs = new \App\Models\Jobs();

    if (!$this->ionAuth->loggedIn())
    {
        return redirect()->to('/logg-inn');
    }

    echo view("dashboard/header", ([
        'ionAuth' => $this->ionAuth,
        'uri' => $this->uri,
    ]));

    echo view("dashboard/jobmarket", ([
        'session' => $this->session,
        'ionAuth' => $this->ionAuth,
        'validation' => $this->validator,
        'jobs' => $this->jobs->all_jobs()->paginate(20),
        'pager' => $this->jobs->pager,
    ]));

    echo view("assets/footer");
}

And here is the correct code for your model:

public function all_jobs() {
    $this->categories = new \App\Models\Categories();
    $builder = $this->db->table('jobs');
    $builder->select('*');
    $builder->join('users', 'users.id = jobs.jobs_u_id');
    $builder->join('categories', 'category_id = jobs.jobs_category');
    // Make sure to not show current user's listings, since these will show up under "My listings"
    $builder->where('jobs_u_id !=', $this->current_user->id);
    // Check that the listing reflects users chosen categories
    $builder->whereIn('category_id', $this->categories->user_categories());
    $builder->orderBy('jobs_id', 'desc');

    return $this;
}

To see the exact place that causes the error try to set CI_ENVIRONMENT = development in your .env file in your root directory. After that try to reload the page where you found this error. You will see a CodeIgniter`s error page with a backtrace. Try to copy all data from backtrace and place here, it helps to understand what exactly is going on.

like image 119
Oleg Demkiv Avatar answered Oct 20 '22 00:10

Oleg Demkiv