protected function buildOptionTree(Category $category, Product $product = null, $level = 0) { $string = ''; for ($i = 0; $i < $level; $i++) { $string .= '— '; } $selected = ''; if ($product) { $product_categories = $product->categories()->get(); foreach ($product_categories as $product_category) { if ($product_category->id == $category->id) { $selected = ' selected'; break; } } } $html = '<option value="' . $category->id . '"' . $selected . '>' . $string . $category->name . '</option>'; $children = $category->children(); if (count($children) > 0) { $level++; foreach ($children as $child) { $html .= $this->buildOptionTree($child, $product, $level); } } return $html; }
/** * Created By Dara on 14/2/2016 * subCategory store */ public function subCategoryStore(Category $category, Request $request) { $this->validate($request, ['name' => 'required']); $category->children()->create(['name' => $request->input('name')]); Flash::success(trans('users.subCategoryCreated')); return redirect(route('admin.category.subCategory.index', $category->id)); }
protected function buildOptionTree(Category $category, Category $editable = null, $level = 0) { $string = ''; for ($i = 0; $i < $level; $i++) { $string .= '— '; } $selected = ''; if ($editable) { $selected = $editable->parent()->id == $category->id ? ' selected' : ''; } $html = '<option value="' . $category->id . '"' . $selected . '>' . $string . $category->name . '</option>'; if (count($category->children()) > 0) { $level++; foreach ($category->children() as $child) { $html .= $this->buildOptionTree($child, $editable, $level); } } return $html; }