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;
});
eg:
$list = CouponOrder::alias('a')->join('car_infor b', 'a.car_id = b.car_id', 'LEFT') ->join('customer c', 'a.customer_id = c.customer_id', 'LEFT')->where($where) ->field(array('a.pici_id', 'a.create_at_str', 'a.remark', 'b.chassis_number', 'c.nickname', 'c.phone', 'a.status', 'a.do_admin_id')) ->group('a.pici_id,a.create_at_str,a.remark,b.chassis_number,c.nickname,c.phone,a.do_admin_id') ->order('a.create_at_str desc') ->paginate($limit, false, ['page' => $page])->each(function ($item, $key) { //取名字 $item['name'] = db('admin_user')->where(array('user_id' => $item['do_admin_id']))->value('name'); $coupon_list = db('coupon_order')->where(array('pici_id' => $item['pici_id']))->column('coupon_no'); $coupon_name = db('coupon')->where('coupon_no', 'IN', $coupon_list)->column('coupon_name'); foreach ($coupon_name as $k => $v) { $coupon_name[$k] = $v . "<br>"; } $item['coupon_name'] = implode(' ', $coupon_name); return $item; });