聚合

在模型中也可以調(diào)用數(shù)據(jù)庫的聚合方法進(jìn)行查詢,例如:

方法 說明
count 統(tǒng)計數(shù)量,參數(shù)是要統(tǒng)計的字段名(可選)
max 獲取最大值,參數(shù)是要統(tǒng)計的字段名(必須)
min 獲取最小值,參數(shù)是要統(tǒng)計的字段名(必須)
avg 獲取平均值,參數(shù)是要統(tǒng)計的字段名(必須)
sum 獲取總分,參數(shù)是要統(tǒng)計的字段名(必須)

靜態(tài)調(diào)用:

User::count();
User::where('status','>',0)->count();
User::where('status',1)->avg('score');
User::max('score');

動態(tài)調(diào)用:

$user = new User;
$user->count();
$user->where('status','>',0)->count();
$user->where('status',1)->avg('score');
$user->max('score');
文檔最后更新時間:2018-04-26 10:05:08

文檔
目錄

深色
模式

切換
寬度