分析分页逻辑
model层查询分页的方法
public function getNews($condition) {
$order = ['id' => 'desc'];
// 查询
$result = $this->where($condition)
->order($order)
->select();
// 打印sql语句
echo $this->getLastSql();
return $result;
}
公共函数common.php
// 应用公共文件
function pagination($obj)
{
if (!$obj) {
return '';
}
//得到参数,并传递
$params = request()->param();
return '<div class="imooc-app">' . $obj->appends($params)->render() . '</div>';
}
静态页面调用分页函数
{:pagination($news)}
报错

解决方案
将model层中的获取分页方法修改
$result = $this->where($condition)
->order($order)
->paginate();
将select()修改为paginate()
后台查看
点击第2页 可分页


本文针对ThinkPHP5在使用分页时出现的'Call to a member function appends() on null'错误进行分析。通过检查分页逻辑,发现在Model层中,原始的查询方法使用了`select()`,应更改为`paginate()`以实现正确的分页功能。修改后,后台查看已成功实现可分页浏览。
341

被折叠的 条评论
为什么被折叠?



