ThinkPHP5 多表查询填充数据再分页的方法

in PHP with 0 comment

thinkphp5取得分页数据后,如果再进行追加数据的话,pageinate应该用each()来操作,这样就能实现数据统计、计算、追加!

$list=db('proxy')
        ->alias('a')
        ->join('user b','a.uid=b.id')
        ->where(['a.pid'=>$this->user['id']])
        ->field('a.id,a.status,a.uid,b.mobile,b.qq,b.shop_name')
        ->order('a.id desc')
        ->paginate(10)
        ->each(function($item,$key){

            $start_time = strtotime(date("Y-m-d",strtotime("-1 day")));   //昨天开始时间
            //$start_time = strtotime(date("Y-m-d",time()));   //今天开始时间
            $end_time = $start_time+24 * 60 * 60-1;  //结束时间

            $w_money = array(
                'create_at'=>['between',[$start_time,$end_time]],
                'user_id' =>['=',$item['uid']],
                'business_type'=>['=','goods_sold']
            );
            $item['day_money'] = Db::name('user_money_log')->where($w_money)->sum('money');

            $orderinfo_sql = 'SELECT count(id) as day_order,sum(quantity) as day_num FROM `order` WHERE user_id = '.$item['uid'].' and create_at>'.$start_time.' and create_at<'.$end_time.' and status=1';
            $orderinfo = Db::query($orderinfo_sql);
            $orderinfo_sql2 = 'SELECT count(select_id) as day_xuanhao FROM `order` WHERE user_id = '.$item['uid'].' and create_at>'.$start_time.' and create_at<'.$end_time.' and status=1 and select_id=""';
            $orderinfo2 = Db::query($orderinfo_sql2);
            $item['day_order'] = $orderinfo[0]['day_order'];//昨日订单量
            $item['day_xuanhao'] = $orderinfo[0]['day_order']-$orderinfo2[0]['day_xuanhao'];//总订单-没选号的数量
            $item['day_num'] = $orderinfo[0]['day_num']; //销卡总量
            return $item;
        });