/** * Set components state based on parameters * * @throws ModelNotFoundException */ public function setState() { $showSlugFilter = $this->property('showSlugFilter'); if (!empty($showSlugFilter)) { $this->show = Show::query()->where('slug', $showSlugFilter)->firstOrFail(); $this->episode = $this->show->episodes()->getQuery()->where('published', true)->orderBy('release', 'desc')->with(['releases', 'releases.release_type', 'image', 'tags', 'show'])->firstOrFail(); } else { $this->episode = Episode::query()->where('published', true)->orderBy('release', 'desc')->with(['releases', 'releases.release_type', 'image', 'tags', 'show'])->firstOrFail(); } $this->releases = Collection::make($this->episode->releases); // Creates a copy $this->releases->sort(function (Release $a, Release $b) { // Order by the sort_order column $aRating = $a->release_type->sort_order; $bRating = $b->release_type->sort_order; return $aRating - $bRating; }); }
/** * If a show is requested by slug loads it, or null if all shows (left blank) * * @throws ModelNotFoundException * @return Show|null */ protected function loadShow() { $slug = $this->property('showSlug'); if ($slug) { return Show::query()->where('slug', $this->property('showSlug'))->firstOrFail(); } else { return null; } }
/** * Set components state based on parameters * * @throws ModelNotFoundException */ public function setState() { $this->show = $show = Show::query()->where('slug', $this->property('showSlug'))->firstOrFail(); $this->releaseType = $releaseType = ReleaseType::query()->where('slug', $this->property('releaseTypeSlug'))->firstOrFail(); $this->episodes = $this->show->episodes()->with(['releases' => function (Relation $query) use($releaseType) { /** @var Relation|Builder $query */ $query->where('release_type_id', $releaseType->id); }])->where('published', true)->whereHas('releases', function (Builder $query) use($releaseType) { $query->where('release_type_id', $releaseType->id); })->orderBy('release', 'desc')->take(intval($this->property('itemLimit')))->get(); $this->episodes->map(function (Episode $episode) use($show) { $episode->setRelation('show', $show); }); }