Skip to content

Instantly share code, notes, and snippets.

@nathangross
Created May 4, 2017 22:17
Show Gist options
  • Save nathangross/745bb94208f313c6636c18bafc9209bb to your computer and use it in GitHub Desktop.
Save nathangross/745bb94208f313c6636c18bafc9209bb to your computer and use it in GitHub Desktop.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Relations\Relation;
Relation::morphMap([
'event' => 'App\Event',
'tournament' => 'App\Tournament'
]);
class Commitment extends Model
{
public function commitable()
{
return $this->morphTo();
}
public function user()
{
return $this->belongsTo(User::class);
}
}
<?php
namespace App;
use App\Sport;
use App\Venue;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
abstract class Event extends Model
{
protected $guarded = [];
// First override default Carbon Dates
protected $dates = ['start_time'];
// Now, make start_time string Carbon date
public function setStarttimeAttribute($date)
{
$this->attributes['start_time'] = Carbon::parse($date);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function sport()
{
return $this->belongsTo(Sport::class);
}
public function venue()
{
return $this->belongsTo(Venue::class);
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
public function commitments()
{
return $this->morphMany(Commitment::class, 'commitable');
}
// public function commitedPlayers()
// {
// // return $this->commitments()->withCount();
// // return $event->commitments()->withCount();
// // return $this->commitments()->count();
// return $this->commitments()->count();
// }
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Event;
class Tournament extends Event
{
// public function commitedPlayers()
// {
// return $this->tournament->commitments()->count();
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment