/** * Rebuilds the tree: update descendants and their order * @param type $categories * @return type */ public static function rebuildTree($categories) { if (is_array($categories)) { foreach ($categories as $cat) { $node = Category::find($cat['id']); //$node->descendants->linknodes(); //loop recursively through the children if (isset($cat['children']) && is_array($cat['children']) && count($cat['children'])) { foreach ($cat['children'] as $child) { //append the children to their (old/new)parents $descendant = Category::find($child['id']); $node->appendNode($descendant); //shift the descendants to the bottom to get the right order at the end $shift = count($descendant->getSiblings()); $descendant->down($shift); Category::rebuildTree($cat['children']); } } } } }
/** * Update Category * * @param ShopCategoryUpdateRequest $request * @param $category * @return \Illuminate\Http\RedirectResponse */ public function update(ShopCategoryUpdateRequest $request, Category $category) { $category->update(['name' => $request->get('name'), 'content' => $request->get('content')]); if ($request->has('parent_id')) { $parent = Category::find($request->get('parent_id')); $parent->children()->save($category); } else { $category->parent_id = null; $category->save(); } if ($request->has('fields')) { $category->fields()->sync($request->get('fields')); } if ($request->has('photos')) { $photos = Photo::whereIn('id', $request->get('photos'))->get(); $category->photos()->saveMany($photos); } Session::flash('message', 'Категория изменено'); return redirect()->route('manager.shop.category.index'); }