gpt4 book ai didi

php - 尝试在 Codeigniter 4 中启动分页时返回错误

转载 作者:行者123 更新时间:2023-12-03 16:49:15 24 4
gpt4 key购买 nike

我正在尝试按照 documentation 在 Codeigniter 4 中启动分页功能.

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

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

我的 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->paginate(20)->all_jobs(),
'pager' => $this->jobs->pager()->all_jobs(),
]));

echo view("assets/footer");

}

但是,在运行它时,我收到以下错误:
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 44 7

这是我的模型
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;
}
}

任何解决此问题的帮助将不胜感激。

最佳答案

我不知道你到底在哪里得到这个错误,但我在你的代码中发现了一些错误。尝试修复这些错误,也许对您有所帮助。以下是错误:

  • paginate()方法返回结果,因此它必须是链中的最后一个。
    示例:$this->jobs->all_jobs()->paginate(20)
  • 您可以获得 Pager像这样:$this->jobs->pager
  • 如果您想使用您的 all_jobs()方法与 paginate()方法,你必须在你的 all_jobs() 中返回一个模型方法

  • 这是您的 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");
    }

    这是您模型的正确代码:
    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;
    }

    要查看导致错误的确切位置,请尝试设置 CI_ENVIRONMENT = development在您的 .env根目录下的文件。之后尝试重新加载发现此错误的页面。您将看到带有回溯的 CodeIgniter 错误页面。尝试从回溯中复制所有数据并将其放在这里,这有助于了解到底发生了什么。

    关于php - 尝试在 Codeigniter 4 中启动分页时返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61170897/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com