find($id); } /** * @return array */ public function getCategoryOptions(): array { $locale = service('request') ->getLocale(); $cacheName = "category_options_{$locale}"; if (! ($options = cache($cacheName))) { $categories = $this->findAll(); $options = array_reduce( $categories, function (array $result, Category $category): array { $result[$category->id] = ''; if ($category->parent !== null) { $result[$category->id] = lang('Podcast.category_options.' . $category->parent->code) . ' › '; } $result[$category->id] .= lang('Podcast.category_options.' . $category->code); return $result; }, [], ); cache() ->save($cacheName, $options, DECADE); } return $options; } /** * Sets categories for a given podcast * * @param int[] $categoriesIds * * @return int|false Number of rows inserted or FALSE on failure */ public function setPodcastCategories(int $podcastId, array $categoriesIds = []): int | false { cache()->delete("podcast#{$podcastId}_categories"); // Remove already previously set categories to overwrite them $this->db ->table('podcasts_categories') ->delete([ 'podcast_id' => $podcastId, ]); if ($categoriesIds === []) { // no row has been inserted after deletion return 0; } // prepare data for `podcasts_categories` table $data = array_reduce( $categoriesIds, function (array $result, int $categoryId) use ($podcastId): array { $result[] = [ 'podcast_id' => $podcastId, 'category_id' => $categoryId, ]; return $result; }, [], ); // Set podcast categories return $this->db->table('podcasts_categories') ->insertBatch($data); } /** * Gets all the podcast categories * * @return Category[] */ public function getPodcastCategories(int $podcastId): array { $cacheName = "podcast#{$podcastId}_categories"; if (! ($categories = cache($cacheName))) { $categories = $this->select('categories.*') ->join('podcasts_categories', 'podcasts_categories.category_id = categories.id') ->where('podcasts_categories.podcast_id', $podcastId) ->findAll(); cache() ->save($cacheName, $categories, DECADE); } return $categories; } }