/** * @api {get} /categories/:slug Get Category Info * @apiGroup Categories * @apiDescription Returns information about a single category, including stickers and subcategories it * contains. * * @param Category $category * * @return \Illuminate\Http\Response */ public function show(Category $category) { $category->load(['subcategories', 'stickers']); $array = $category->toArray(); // Have to do this manually because if parentCategory is loaded with ->load it comes out in snake_case $array['parentCategory'] = $category->getParentCategory(); return $this->response(['category' => $array]); }
/** * Returns an array of $id => $name values for every category to display in an HTML select. * * @param bool $idInName * * @return array */ public function getSelectList($idInName = true) { $categories = Cache::rememberForever('categorySelectList', function () { $list = []; /** @var Category[] $cats */ $cats = Category::topLevel()->orderBy('name')->with('subcategories')->get(); /** * @param array $list * @param Category[] $cats * @param int $level */ function appendCats(&$list, $cats, $level = 0) { foreach ($cats as $cat) { $list[$cat->id] = str_repeat(" ", $level) . $cat->name; appendCats($list, $cat->subcategories()->orderBy('name')->get(), $level + 1); } } appendCats($list, $cats); return $list; }); if ($idInName) { foreach ($categories as $id => $name) { $categories[$id] = $name . " [{$id}]"; } } return $categories; }
protected function getList() { if ($this->request->has('category')) { $categoryId = $this->request->input('category'); /** @var Category $category */ $category = Category::findOrFail($categoryId); $this->pageTitle = ' Stickers in ' . $category->name; $this->breadcrumbs = \Breadcrumbs::render('category-stickers', $category); return Sticker::where('categoryId', $categoryId)->orderBy('name'); } else { return Sticker::orderBy('name'); } }
protected function getSearchableList() { return Category::query(); }