summaryrefslogtreecommitdiff
path: root/app/Rugby/Model
diff options
context:
space:
mode:
Diffstat (limited to 'app/Rugby/Model')
-rw-r--r--app/Rugby/Model/Match.php59
-rw-r--r--app/Rugby/Model/Team.php34
-rw-r--r--app/Rugby/Model/Tournament.php19
-rw-r--r--app/Rugby/Model/Venue.php17
4 files changed, 129 insertions, 0 deletions
diff --git a/app/Rugby/Model/Match.php b/app/Rugby/Model/Match.php
new file mode 100644
index 0000000..5447ef5
--- /dev/null
+++ b/app/Rugby/Model/Match.php
@@ -0,0 +1,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');
+ }
+}
diff --git a/app/Rugby/Model/Team.php b/app/Rugby/Model/Team.php
new file mode 100644
index 0000000..2582f33
--- /dev/null
+++ b/app/Rugby/Model/Team.php
@@ -0,0 +1,34 @@
+<?php
+
+namespace App\Rugby\Model;
+
+use Illuminate\Database\Eloquent\Model;
+
+use App\Rugby\Model\Match;
+
+class Team extends Model
+{
+ protected $table = 'teams';
+
+ protected $fillable = ['name'];
+
+ public function matches()
+ {
+ return $this->belongsToMany(Match::class)->withPivot('is_home');
+ }
+
+ public function homeMatches()
+ {
+ return $this->matches()->wherePivot('is_home', '=', true);
+ }
+
+ public function awayMatches()
+ {
+ return $this->matches()->wherePivot('is_home', '=', false);
+ }
+
+ public function getName()
+ {
+ return $this->name ?: '';
+ }
+}
diff --git a/app/Rugby/Model/Tournament.php b/app/Rugby/Model/Tournament.php
new file mode 100644
index 0000000..64d9a45
--- /dev/null
+++ b/app/Rugby/Model/Tournament.php
@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Rugby\Model;
+
+use App\Rugby\Model\Match;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Tournament extends Model
+{
+ protected $table = 'tournaments';
+
+ protected $fillable = ['name'];
+
+ public function matches()
+ {
+ return $this->belongsToMany(Match::class);
+ }
+}
diff --git a/app/Rugby/Model/Venue.php b/app/Rugby/Model/Venue.php
new file mode 100644
index 0000000..be85c23
--- /dev/null
+++ b/app/Rugby/Model/Venue.php
@@ -0,0 +1,17 @@
+<?php
+
+namespace App\Rugby\Model;
+
+use App\Rugby\Model\Match;
+
+use Illuminate\Database\Eloquent\Model;
+
+class Venue extends Model
+{
+ protected $fillable = ['name', 'city'];
+
+ public function matches()
+ {
+ return $this->belongsToMany(Match::class);
+ }
+}