tp5新手村:RC4 模型里分页

浏览:1850 发布日期:2016/07/12 分类:ThinkPHP5专区 关键字: tp5新手村 分页
很多小伙伴在tp5里的分页 遇到了问题我就把代码分享出来一下
model里的代码:/*
     * 菜单列表
     * @param int $pid 父级ID
     * @author staitc7
     */

    public function menuList(int $pid = 0): array {
        $map = $pid === 0 ? ['pid' => 0] : ['pid' => $pid];
        $map['status'] = ['neq', -1];
//        $object = $this::all(function($query)use($map) {
//                    $query->where($map)->order('sort asc,id asc')->limit(10);
//                });//闭包里使用分页是个坑 默认全表查询 不要用
        $object = $this::where($map)->order('sort asc,id asc')->paginate(10);
        return $object ? array_merge($object->toArray(),['page'=>$object->render()]): [];
    }
控制器的代码如下 /*
     * 菜单首页管理
     * @param int $pid 父级ID
     * @author staitc7
     */

    public function index($pid = 0) {
        $Menu = Loader::model('Menu');
        $data = $Menu->menuList((int) $pid);
        $value = [
            'data' => $data['data'],
            'page' => $data['page']
        ];
        $view = new View([], Config::get('replace_str'));
        $view->meta_title = '菜单列表';
        return $view->assign($value)->fetch();
    }
注意php版本。我是用的php7,php7应该成为框架里常态
最佳答案
评论( 相关
后面还有条评论,点击查看>>