zoukankan      html  css  js  c++  java
  • THINKPHP5 分页数据对象的处理【转】

    在用到THINKPHP5的分页的时候,我们可以发现获取的数据是对象,如果我们要对数据进行循环增加数据就实现不了

    今天用此方法解决,以做记录方便以后忘了查看

    // 查询状态为1的用户数据 并且每页显示30条数据
    $list = db('commission_log')->order('id desc')->paginate(30);
    // 获取分页显示
    $page = $list->render();
    $list = $list->all();        //这是关键
    // 模板变量赋值
    foreach ($list as $k => $v) {
        //通过用户ID查询代理名
        $list[$k]['username'] = db('userinfo')->where(array('uid'=>$v['uid']))->value('username');
    }
    $this->assign('list', $list);
    $this->assign('page', $page);
    // 渲染模板输出
    return $this->fetch();
    

    分页后数据处理 (V5.0.9

    V5.0.9版本开始 支持分页类后数据直接each遍历处理,方便修改分页后的数据,而不是只能通过模型的获取器来补充字段。

    $list = User::where('status',1)->paginate()->each(function($item, $key){
        $item->nickname = 'think';
    });
    
    

    如果是Db类操作分页数据的话,each方法的闭包函数中需要使用返回值,例如:

    $list = Db::name('user')->where('status',1)->paginate()->each(function($item, $key){
        $item['nickname'] = 'think';
        return $item;
    });
    

    文章来自:
    https://www.cnblogs.com/lhm166/articles/8678854.html
    https://www.kancloud.cn/manual/thinkphp5/154294

  • 相关阅读:
    MaaS系统概述
    流处理认识
    事务补偿
    Hystrix原理与实战
    RPC概念和框架
    git remote: error: hook declined to update
    Unity CombineTexture
    Windows Powershell统计代码行数
    unity面试题二
    unity面试题一
  • 原文地址:https://www.cnblogs.com/KillBugMe/p/12409826.html
Copyright © 2011-2022 走看看