企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
#### **一.关联统计** #### ***1.***使用withCount()方法,可以统计主表关联附表的个数,输出用profile\_count; $list = UserModel::withCount('profile')->all(\[1,2,3\]); //值得注意的是,比如withCount('profile')这个表,然后会有一个默认变量,profile\_count,这个数据就是统计的数据,这个是默认的,当然,也可以自定义 foreach($list as $user){ echo $user->profile\_count;//默认变量数据 } withCount(\['profile'=>'**ps**'\])->all(\[1,2,3\]); 这个ps就是自定义的统计变量了 #### ***2.***关联统计的输出采用"关联方法名"\_count,这种结构输出 #### ***3.***不单单支持Count,还有这些统计方法, withMax(),withMin(),withSum(),withAvg() #### ***4.***除了withhCount()不需要指定字段,其他的统计方法都需要指定字段 $list = UserModel::withSum('profile','status')->all(\[1,2,3\])