Completed
Push — master ( d49014...393727 )
by Vincenzo
02:54
created

Player::avg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace App\Lib\DsManager\Models\Orm;
4
5
/**
6
 * Class Player
7
 * @package App\Lib\DsManager\Models
8
 */
9
class Player extends DsManagerOrm
10
{
11
12
    /**
13
     * @var string
14
     */
15
    protected $table = 'players';
16
17
    /**
18
     * @var array
19
     */
20
    protected $fillable = [
21
        'name',
22
        'surname',
23
        'age',
24
        'nationality',
25
        'skillAvg',
26
        'wageReq',
27
        'val',
28
        'role',
29
        'team_id'
30
    ];
31
32
    /**
33
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34
     */
35
    public function team()
36
    {
37
        return $this->belongsTo(Team::class);
38
    }
39
40
    public function lastMatches()
41
    {
42
        return $this->hasMany(MatchPlayer::class)
43
            ->orderBy('updated_at', 'DESC')
44
            ->limit(5);
45
    }
46
47
    public function goals()
48
    {
49
        return $this->hasOne(MatchPlayer::class)
50
            ->selectRaw('player_id, sum(goals) as count')
51
            ->groupBy('player_id');
52
    }
53
54
    public function appearances()
55
    {
56
        return $this->hasOne(MatchPlayer::class)
57
            ->selectRaw('player_id, count(match_id) as count')
58
            ->groupBy('player_id');
59
    }
60
61
    public function avg()
62
    {
63
        return $this->hasOne(MatchPlayer::class)
64
            ->selectRaw('player_id, round(avg(vote),2) as avg')
65
            ->groupBy('player_id');
66
    }
67
68
    public function scopeStatistics($query)
69
    {
70
        return $query->with(
71
            'goals',
72
            'appearances',
73
            'avg',
74
            'lastMatches',
75
            'team'
76
        );
77
    }
78
}