public function getCategories($isTable = false) { $categories = $isTable ? ProductsCategory::where('parent_id', '=', '0')->get() : ProductsCategory::where('parent_id', '=', '0')->get(array('id', 'title')); $outputArr = [0 => 'no category']; foreach ($categories as $category) { $title = $category->title; if ($isTable) { $outputArr[$category->id]['title'] = $title; $outputArr[$category->id]['tree'] = $title; } else { $outputArr[$category->id] = $title; } $childs = $category->childs; foreach ($childs as $child) { $titleChild = $title . ' > ' . $child->title; if ($isTable) { $outputArr[$child->id]['title'] = $child->title; $outputArr[$child->id]['tree'] = $titleChild; } else { $outputArr[$child->id] = $titleChild; } $grandChilds = $child->childs; foreach ($grandChilds as $grandChild) { $titleGrandChild = $titleChild . ' > ' . $grandChild->title; if ($isTable) { $outputArr[$grandChild->id]['title'] = $grandChild->title; $outputArr[$grandChild->id]['tree'] = $titleGrandChild; } else { $outputArr[$grandChild->id] = $titleGrandChild; } } } } return $outputArr; }
public function getCategory($id) { $category = ProductsCategory::find($id); if (!$category) { return redirect()->to('shop'); } $pageData = self::getPageElements($category->title, $category->title, '', '', true, false, false, $category->mainParent()->id); $products = $category->products; $pageData['products'] = $products; $pageData['category'] = $category; return view('shop', $pageData); }
protected function getPageElements($current, $title, $description, $keywords, $categories = false, $products = false, $promo = false, $currentCategory = null, $perPage = 12) { $data = ['current' => $current, 'title' => $title, 'description' => $description, 'keywords' => $keywords, 'current_category' => $currentCategory]; if ($categories) { $data['categories'] = App\ProductsCategory::where('parent_id', '=', 0)->get(); } if ($products) { $productsResponse = Product::paginate($perPage); $products = $productsResponse->getCollection()->all(); $pages = $productsResponse->render(); $data['products'] = $products; $data['pages'] = $pages; } if ($promo) { $data['promo_products'] = Product::where('on_discount', '<>', 0)->get()->take(3); } return $data; }
/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { if (!Auth::check()) { return redirect()->to('panel/login'); } $category = ProductsCategory::find($id); $childs = $category->childs; foreach ($childs as $child) { $child->delete(); } $category->delete(); return redirect()->back(); }
public static function checkExisting($category, $id) { if (!$category) { $categories = ProductsCategory::all(); if (!$categories->count()) { return ['error' => ['code' => 1, 'type' => 'noRecords', 'data' => ['message' => 'No categories exist.', 'links' => [['url' => 'panel/products/categories', 'text' => 'Go to table ']]]]]; } $firstCategory = $categories[0]; $middleCategory = $categories[intval((count($categories) - 1) / 2)]; $lastCategory = $categories[count($categories) - 1]; $firstDiff = abs($firstCategory->id - $id); $middleDiff = abs($middleCategory->id - $id); $lastDiff = abs($lastCategory->id - $id); if ($firstDiff < $middleDiff && $firstDiff < $lastDiff) { $categoryLF = $firstCategory; } else { if ($firstDiff > $middleDiff && $middleDiff < $lastDiff) { $categoryLF = $middleCategory; } else { if ($firstDiff > $lastDiff && $middleDiff > $lastDiff) { $categoryLF = $lastCategory; } else { $categoryLF = $firstCategory; } } } $links = [['url' => 'panel/products/categories', 'text' => 'Go to table'], ['url' => 'panel/products/categories/' . $categoryLF->id . '/edit', 'text' => 'May be you looking for "' . $categoryLF->title . '" category.']]; if ($firstDiff != $middleDiff && $firstDiff != $lastDiff && $middleDiff != $lastDiff) { $links[2] = ['url' => null, 'text' => 'Or you want other categories?']; $links[3] = ['url' => 'panel/products/categories/' . $firstCategory->id . '/edit', 'text' => '1. "' . $firstCategory->title . '" category.']; $links[4] = ['url' => 'panel/products/categories/' . $middleCategory->id . '/edit', 'text' => '2. "' . $middleCategory->title . '" category.']; $links[5] = ['url' => 'panel/products/categories/' . $lastCategory->id . '/edit', 'text' => '3. "' . $lastCategory->title . '" category.']; } return ['error' => ['code' => 1, 'type' => 'noExist', 'data' => ['message' => 'Category with this id does not exist.', 'links' => $links]]]; } return ['error' => ['type' => 'none', 'code' => 0]]; }