2016-11-28 08:40:46 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
2017-01-06 09:40:27 +01:00
|
|
|
use App\Services\TMDB;
|
2016-11-28 08:40:46 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class Episode extends Model {
|
|
|
|
|
|
|
|
public $timestamps = false;
|
|
|
|
|
|
|
|
protected $fillable = [
|
|
|
|
'tmdb_id',
|
|
|
|
'name',
|
|
|
|
'season_number',
|
|
|
|
'episode_number',
|
|
|
|
'episode_tmdb_id',
|
|
|
|
'seen',
|
|
|
|
'season_tmdb_id',
|
2017-01-06 13:00:18 +01:00
|
|
|
'created_at',
|
2016-11-28 08:40:46 +01:00
|
|
|
];
|
2017-01-06 09:40:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Save all episodes of each season.
|
|
|
|
*
|
|
|
|
* @param $seasons
|
|
|
|
* @param $tmdbId
|
|
|
|
*/
|
|
|
|
public function store($tmdbId, TMDB $tmdb)
|
|
|
|
{
|
|
|
|
$seasons = $tmdb->tvEpisodes($tmdbId);
|
|
|
|
|
|
|
|
foreach($seasons as $season) {
|
|
|
|
foreach($season->episodes as $episode) {
|
|
|
|
$this->create([
|
|
|
|
'season_tmdb_id' => $season->id,
|
|
|
|
'episode_tmdb_id' => $episode->id,
|
|
|
|
'season_number' => $episode->season_number,
|
|
|
|
'episode_number' => $episode->episode_number,
|
|
|
|
'name' => $episode->name,
|
|
|
|
'tmdb_id' => $tmdbId,
|
|
|
|
'created_at' => time(),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Scopes
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function scopeFindByTmdbId($query, $tmdbId)
|
|
|
|
{
|
|
|
|
return $query->where('tmdb_id', $tmdbId);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeFindEpisode($query, $tmdbId, $episode)
|
|
|
|
{
|
|
|
|
return $query->where('tmdb_id', $tmdbId)
|
|
|
|
->where('season_number', $episode->season_number)
|
|
|
|
->where('episode_number', $episode->episode_number);
|
|
|
|
}
|
2016-11-28 08:40:46 +01:00
|
|
|
}
|