summaryrefslogtreecommitdiff
path: root/app/Rugby/Model/Match.php
blob: 58a3dd92770a8c39cd4881319af5d897fe69d943 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php

namespace App\Rugby\Model;

use Illuminate\Database\Eloquent\Model;

class Match extends Model
{
    protected $table = 'matches';
    protected $casts = ['date' => 'datetime:Y-m-d'];
    protected $fillable = ['score', 'half_score', 'referee', 'date'];

    public function teams()
    {
        return $this->belongsToMany(Team::class, 'match_team');
    }

    public function venue()
    {
        return $this->hasOne(Venue::class, 'id');
    }

    public function getDisplayName()
    {
        $venue = Venue::find($this->venue_id);

        if (!$venue) {
            return 'Unknown';
        }

        return $venue->name;
    }

    public function getDisplayDate()
    {
        return (new \Carbon\Carbon($this->date))->format('M d Y');
    }

    public function getVideoUrl()
    {
        $video = $this->videos()->first();

        if (! $video) {
            return '';
        }

        return $video->getUrl();
    }

    public function homeTeam()
    {
        return $this->teams()->wherePivot('is_home', '=', true);
    }

    public function awayTeam()
    {
        return $this->teams()->wherePivot('is_home', '=', false);
    }

    public function tournaments()
    {
        return $this->belongsToMany(Tournament::class, 'match_tournament');
    }

    public function videos()
    {
        return $this->hasMany(Video::class, 'match_id');
    }

}