使用查询范围。它们非常方便
class Stats extends Eloquent{
public function scopefileStats($query, $timeFrame){
$query->where('user_id', = ,Auth::user()->id);
$query->where('created_at','>', DB::raw($timeFrame));
$query->where('type','=','FILE');
}
public function scopelinkStats($query,$timeFrame){
$query->where('user_id', = ,Auth::user()->id);
$query->where('created_at','>', DB::raw($timeFrame));
$query->where('type','=','LINK');
}
public function scopereferralStats($query,$timeFrame){
$query->where('user_id', = ,Auth::user()->id);
$query->where('created_at','>', DB::raw($timeFrame));
$query->where('type','=','REFERRAL');
}
}
检索数据为
Stats::fileStats('date_sub(NOW(), INTERVAL 1 MONTH)')->sum('amount');
Stats::linkStats('date_sub(NOW(), INTERVAL 1 MONTH)')->sum('amount');
Stats::referralStats('date_sub(NOW(), INTERVAL 1 MONTH)')->sum('amount');
这可能是您可以执行此操作的方法之一,但您始终可以根据自己的舒适度自定义上述代码。