查询的时候,直接 在下面添加 一个字段就行!
public function getDeatil(){$post = $this->request->post();if(!isset($post['id'])){return out('请传递活动的id');}$builder = new Builder($this->getModel());$builder->setFilter(['id' => $post['id']]);$list = $builder->selectQuery(function (BaseQuery $query) {//关联查询-开始$with = array_merge($with ?? [], ['template', 'user','type']);$query->with($with);$append = array_merge($append ?? [], ['place', 'place_text']);$query->append($append);//关联查询-结束$query->append(['donation','currentpower','goalpower','benefit_text'])->withAttr(['donation' => function ($v, $r) {$tem = [];$type = (new InfoModel())->where('id',$r['id'])->value('lx');$data = (new RechargeModel())->with('user')->where('list_id',$type)->field('user_id,plan_id,num,pay_time')->select()->toArray();foreach ($data as $k => $item){if(isset($item['user'])){$tem[$k]['user'] = $item['user']['user_name'];}if(isset($item['num'])){$tem[$k]['num'] = $item['num'];}if(isset($item['plan_id'])){$plan_model = new PlanModel();$plan_name = $plan_model->where('id',$item['plan_id'])->column(['plan_name']);$tem[$k]['plan_name'] = $plan_name;}$tem[$k]['pay_time'] = $item['pay_time'];}return $tem;},'currentpower' => function ($v, $r) {$data = (new RechargeModel())->with(['user'])->where(['list_id' =>$r['id'] ,'status' =>1])->field('SUM(power) as total_num')->select()->toArray();$currentpower = array_reduce($data, function ($carry,$item) {return $item;}, []);if( $currentpower['total_num'] == null){$currentpower['total_num'] = 0;}return $currentpower['total_num'];},'goalpower' => function ($v, $r) {$type = (new InfoModel())->where('id',$r['id'])->value('power');return $type;},]);})->page();return $list;}