public function testRootsStatic() { $query = Category::whereNull('parent_id')->get(); $roots = Category::roots()->get(); $this->assertEquals($query->count(), $roots->count()); $this->assertCount(2, $roots); foreach ($query->lists('id') as $node) { $this->assertContains($node, $roots->lists('id')); } }
/** * Admin view */ public function index() { $initialCategories = Category::whereNull('parent_id')->with(['categories' => function ($query) { $query->orderBy('weight', 'desc')->orderBy('id'); }])->orderBy('weight', 'desc')->orderBy('id')->paginate(); $links = $initialCategories->links(); $result = new \Illuminate\Database\Eloquent\Collection(); $initialCategories->each(function ($category) use($result) { $result->push($category); $category->categories->each(function ($category) use($result) { $result->push($category); }); }); $result->load('parent'); // dd($result); return View::make('admin.dicts.list', ['title' => 'Категории', 'columns' => ['ID', 'Родитель', 'Вес', 'Заголовок', 'Описание', 'Под', 'Посты', 'Ком', 'посты', '', ''], 'data' => $result->transform(function ($category) use($links) { return ['id' => $category->id, 'parent_id' => $category->parent_id ? "{$category->parent->title} ({$category->parent_id})" : '', 'weight' => $category->weight . ' ' . link_to("admin/categories/{$category->id}/up", "↑") . ' ' . link_to("admin/categories/{$category->id}/down", "↓"), 'title' => $category->title, 'description' => $category->description, 'subscriptions' => $category->subscriptions_count, 'posts_count' => $category->posts_count, 'comments' => $category->comments_count, 'posts' => link_to("/admin/categories/{$category->id}/posts", 'посты →'), 'edit' => link_to("/admin/categories/{$category->id}/edit", 'редактировать'), 'delete' => link_to("/admin/categories/{$category->id}/delete", 'удалить')]; }), 'links' => $links, 'actions' => [['link' => '/admin/categories/create', 'text' => 'Добавить']]]); }
public function getHierarchy($blog_category_id = 0, $depth = 0, $template = 'blog::partials.tr.category', $selected = 1) { $hierarchy = ''; if ($blog_category_id) { $categories = Category::where('id', '=', $blog_category_id)->orderBy('order', 'asc')->get(); } else { $categories = Category::whereNull('blog_category_id')->orderBy('order', 'asc')->get(); } foreach ($categories as $category) { $data = array('depth' => $depth, 'category' => $category, 'selected' => $selected, 'max_order' => Category::where('blog_category_id', '=', $category->blog_category_id)->count()); $hierarchy .= \View::make($template, $data); if ($template == 'blog::partials.tr.category') { foreach ($category->articles()->orderBy('order', 'asc')->get() as $article) { $data = array('depth' => $depth + 1, 'article' => $article, 'max_order' => Article::where('blog_category_id', '=', $article->blog_category_id)->count()); $hierarchy .= \View::make('blog::partials.tr.article', $data); } } foreach ($category->categories()->orderBy('order', 'asc')->get() as $category) { $hierarchy .= $this->getHierarchy($category->id, $depth + 1, $template, $selected); } } return $hierarchy; }