public function run() { Post::create(['title' => 'First blog post', 'slug' => 'first-blog-post', 'content' => ' This is your first ever **blog post**! It might be a good idea to update this post with some more relevant content. You can edit this content by selecting **Blog** from the administration back-end menu. *Enjoy the good times!* ', 'excerpt' => 'The first ever blog post is here. It might be a good idea to update this post with some more relevant content.', 'published_at' => Carbon::now(), 'published' => true]); Category::create(['name' => trans('richlove.blog::lang.categories.uncategorized'), 'slug' => 'uncategorized']); }
public function index_onDelete() { if (($checkedIds = post('checked')) && is_array($checkedIds) && count($checkedIds)) { foreach ($checkedIds as $categoryId) { if (!($category = Category::find($categoryId))) { continue; } $category->delete(); } Flash::success('Successfully deleted those categories.'); } return $this->listRefresh(); }
protected function loadCategories() { $categories = BlogCategory::orderBy('name'); if (!$this->property('displayEmpty')) { $categories->whereExists(function ($query) { $query->select(Db::raw(1))->from('richlove_blog_posts_categories')->join('richlove_blog_posts', 'richlove_blog_posts.id', '=', 'richlove_blog_posts_categories.post_id')->whereNotNull('richlove_blog_posts.published')->where('richlove_blog_posts.published', '=', 1)->whereRaw('richlove_blog_categories.id = richlove_blog_posts_categories.category_id'); }); } $categories = $categories->getNested(); /* * Add a "url" helper attribute for linking to each category */ return $this->linkCategories($categories); }
public function up() { if (Schema::hasColumn('richlove_blog_categories', 'parent_id')) { return; } Schema::table('richlove_blog_categories', function ($table) { $table->integer('parent_id')->unsigned()->index()->nullable(); $table->integer('nest_left')->nullable(); $table->integer('nest_right')->nullable(); $table->integer('nest_depth')->nullable(); }); foreach (CategoryModel::all() as $category) { $category->setDefaultLeftAndRight(); $category->save(); } }
public function boot() { /* * Register menu items for the RainLab.Pages plugin */ Event::listen('pages.menuitem.listTypes', function () { return ['blog-category' => 'Blog Category', 'all-blog-categories' => 'All Blog Categories']; }); Event::listen('pages.menuitem.getTypeInfo', function ($type) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::getMenuTypeInfo($type); } }); Event::listen('pages.menuitem.resolveItem', function ($type, $item, $url, $theme) { if ($type == 'blog-category' || $type == 'all-blog-categories') { return Category::resolveMenuItem($item, $url, $theme); } }); }
protected function getCategoryIdsForPost($data) { $ids = []; if ($this->auto_create_categories) { $categoryNames = $this->decodeArrayValue(array_get($data, 'categories')); foreach ($categoryNames as $name) { if (!($name = trim($name))) { continue; } if (isset($this->categoryNameCache[$name])) { $ids[] = $this->categoryNameCache[$name]; } else { $newCategory = Category::firstOrCreate(['name' => $name]); $ids[] = $this->categoryNameCache[$name] = $newCategory->id; } } } elseif ($this->categories) { $ids = (array) $this->categories; } return $ids; }
/** * Lists posts for the front end * @param array $options Display options * @return self */ public function scopeListFrontEnd($query, $options) { /* * Default options */ extract(array_merge(['page' => 1, 'perPage' => 30, 'sort' => 'created_at', 'categories' => null, 'category' => null, 'search' => '', 'published' => true], $options)); $searchableFields = ['title', 'slug', 'excerpt', 'content']; if ($published) { $query->isPublished(); } /* * Sorting */ if (!is_array($sort)) { $sort = [$sort]; } foreach ($sort as $_sort) { if (in_array($_sort, array_keys(self::$allowedSortingOptions))) { $parts = explode(' ', $_sort); if (count($parts) < 2) { array_push($parts, 'desc'); } list($sortField, $sortDirection) = $parts; if ($sortField == 'random') { $sortField = DB::raw('RAND()'); } $query->orderBy($sortField, $sortDirection); } } /* * Search */ $search = trim($search); if (strlen($search)) { $query->searchWhere($search, $searchableFields); } /* * Categories */ if ($categories !== null) { if (!is_array($categories)) { $categories = [$categories]; } $query->whereHas('categories', function ($q) use($categories) { $q->whereIn('id', $categories); }); } /* * Category, including children */ if ($category !== null) { $category = Category::find($category); $categories = $category->getAllChildrenAndSelf()->lists('id'); $query->whereHas('categories', function ($q) use($categories) { $q->whereIn('id', $categories); }); } return $query->paginate($perPage, $page); }
protected function loadCategory() { if (!($categoryId = $this->property('categoryFilter'))) { return null; } if (!($category = BlogCategory::whereSlug($categoryId)->first())) { return null; } return $category; }