laravel中的统计计算

时间:2015-05-26 09:22:04

标签: class statistics laravel-5

我正在开展一个高尔夫统计项目。现在我进入并编辑高尔夫统计数据到目前为止。我正在使用laravel 5 btw。

我的数据库架构的工作原理如下:

  • 您玩的每一轮都会在圆桌会议中保存一个条目,其中包含(日期,播放列表,天气,天际线)等信息。
  • 为每个洞打了一个得分表中的一个条目。在那里我提到了圆圈和信息,如(得分,球道,球场,推杆,点球......)

不,我想创建报告,我可以过滤日期和课程等。 我现在做的是。我创建了一个Statistic类,我可以在构造中传递date,playerid,roundid,courseid。该构造将查询播放的所有轮次,匹配这些过滤器。

然后,foreach统计我做了一个公共功能ex。 scoring_average,greenhit_percantage,putts_per_round,putts_per_greeninregulation等大约有15个统计数据。

所以我的问题是:我在这里做的是对的吗?因为我有大约15个函数来计算统计数据。

如果你有更好的解决方案,请给我一些建议。

谢谢

1 个答案:

答案 0 :(得分:0)

班级统计{

/**
 * The table associated with the model.
 *
 * @var string
 */
public $rounds = [];

public function __construct($user_id, $roundid = null, $start = "2000-01-01", $end = "2030-01-01", $courseid = 0){
    $this->rounds = Round::where('user_id', '=', $user_id)->get();
}

public function score(){
    if(count($this->rounds) > 0){
        $avg = 0;
        foreach($this->rounds as $round){
            $scores = Score::where('round_id', '=', $round->id)->get(['score']);
            foreach($scores as $score){
                $avg += $score->score;
            }
        }

        return $avg / count($this->rounds);
    } else {
        return "N/A";
    }

}

public function fir(){
    if(count($this->rounds) > 0){
        $fairway = [];
        foreach($this->rounds as $round){
            $scores = Score::where('round_id', '=', $round->id)->get(['fir']);
            foreach($scores as $score){
                if($score->fir != 0){
                    array_push($fairway, $score->fir);
                }

            }
        }

        $hits = array_count_values($fairway);

        //unset($hits[0]); //unsets par 3 with value 0

        return self::percArray($hits);

        return $perc;
    } else {
        return "N/A";
    }

}