Ejemplo n.º 1
0
 /**
  * @param Category $category
  *
  * @throws \InvalidArgumentException
  */
 private function addProductsToCategory(Category $category)
 {
     for ($i = 0, $count = random_int(2, 8); $i < $count; $i++) {
         $product = $this->products()->random();
         if ($category->products->contains('id', $product->id)) {
             continue;
         }
         $category->products()->save($product);
         $category->products->add($product);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param int    $id
  * @param string $slug
  *
  * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  *
  * @return View|RedirectResponse
  */
 public function viewCategory(int $id, string $slug)
 {
     /** @var Category $category */
     $category = $this->category->findOrFail(Category::privateId($id));
     if ($category->slug() !== $slug) {
         return $this->webUi->redirect('categories.view', [$category->id, $category->slug()]);
     }
     $tree = $category->getDescendantsAndSelf()->load(['products' => function ($query) {
         /* @var Product $query */
         $query->with(Product::standardRelations());
     }]);
     return $this->webUi->view('customer.category.view', compact('category', 'tree'));
 }
Ejemplo n.º 3
0
 /**
  * @param int     $id
  * @param Request $request
  *
  * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function putCategoryParent(int $id, Request $request)
 {
     /** @var Category $category */
     $category = $this->category->findOrFail($id);
     if ((int) $request->get('parent-id') === -1) {
         $category->makeRoot();
         $this->webUi->successMessage("Made `{$category->name}` a root-level category.");
         return $this->webUi->redirect('categories.index');
     }
     /** @var Category $parent */
     $parent = $this->category->findOrFail($request->get('parent-id'));
     $category->makeChildOf($parent);
     $this->webUi->successMessage("Made `{$category->name}` a child of `{$parent->name}`.");
     return $this->webUi->redirect('categories.index');
 }
Ejemplo n.º 4
0
 /**
  * @return Category
  */
 protected function createCategory() : Category
 {
     return Category::create(['name' => uniqid('Category', false)]);
 }
Ejemplo n.º 5
0
 /**
  * @return Collection|Category[]
  */
 public function loadAllCategories() : Collection
 {
     return $this->category->all();
 }