Thinkphp6 关联 withCount 运用


 $res = $builder->selectQuery(function (BaseQuery $query) use($pt_type,$plate_type,$userdata){
 			//use($a,$b,$c){} 传参
           if ($pt_type == 'h5') {
             $query->withCount(['Formcount' => 'apply_count']);//没有条件时统计
           }else{
               $query->withCount(['Formcount' => function($query, &$alias) use($userdata){
                   $query->where('p_id',$userdata['id']);//统计时条件
                   $alias = 'apply_count';//统计别名
               }]);
           }
     $query->field(['id','p_id','title','title_color','button_font','button_font_color','image','sort','show','apply_count','type','case  type WHEN "0" then "服务申请" when "1" then "办事指南" WHEN "2" then "入学转学评估" ELSE "评估中心" end as typename']);
        })->select();

你可能感兴趣的:(PHP工作后遇到的问题,java,sql,数据库)