summaryrefslogtreecommitdiff
path: root/app/Rugby/Model/Match.php
blob: 5447ef527f60ac6aaceb80560cc558e5397d629b (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
<?php

namespace App\Rugby\Model;

use App\Rugby\Model\Team;
use App\Rugby\Model\Tournament;
use App\Rugby\Model\Venue;

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 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');
    }
}