public function onStart() { $slug = $this->param('slug'); $this['category'] = Category::where('slug', '=', $slug)->first(); if ($this['category']) { $post = new Post(); $query = $post->isPublished()->orderBy('published_at', 'desc')->with('categories'); $this['posts'] = $post->scopeFilterCategories($query, [$this['category']->id])->get(); } }
public function index() { $this->vars['postsTotal'] = Post::count(); $this->vars['postsPublished'] = Post::isPublished()->count(); $this->vars['postsDrafts'] = $this->vars['postsTotal'] - $this->vars['postsPublished']; $this->asExtension('ListController')->index(); }
/** * Load post and start building query for related posts */ public function onRun() { // Load the target post $post = Post::where('slug', $this->property('slug'))->with('tags')->first(); // Abort if there is no source, or it has no tags if (!$post || !($tagIds = $post->tags->lists('id'))) { return; } // Start building our query for related posts $query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($tag) use($tagIds) { $tag->whereIn('id', $tagIds); })->with('tags'); // Sort the related posts $subQuery = DB::raw('( select count(*) from `bedard_blogtags_post_tag` where `bedard_blogtags_post_tag`.`post_id` = `rainlab_blog_posts`.`id` and `bedard_blogtags_post_tag`.`tag_id` in (' . implode(', ', $tagIds) . ') )'); $key = $this->property('orderBy') ?: $subQuery; $query->orderBy($key, $this->property('direction')); // Limit the number of results if ($take = intval($this->property('results'))) { $query->take($take); } // Execute the query $this->posts = $query->get(); }
/** * Add fields to the MenuItem form * * @param Form $form * * @return void */ public function extendItemForm(Form $form) { if (!$this->posts) { $posts = Post::isPublished()->select('id', 'title', 'published_at')->orderBy('created_at', 'desc')->get(); $options = []; foreach ($posts as $post) { $options[$post->id] = date('M j Y H:i', strtotime($post->published_at)) . ' - ' . $post->title; } asort($options); $this->posts = $options; } $form->addFields(['master_object_id' => ['label' => 'Blog Post', 'comment' => 'Select the blog post you wish to link to. Remember to set the Blog Posts Page option on the Menu Settings page!', 'type' => 'dropdown', 'options' => $this->posts, 'tab' => 'Item']], 'primary'); }
private function loadMostViewedPost($numberOfPost) { $posts = Post::isPublished()->where('view', '>', $this->property('minimalView'))->orderBy('view', 'desc')->take($numberOfPost)->get(); /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->property('postPage'), $this->controller); $post->categories->each(function ($category) { $category->setUrl($this->property('categoryPage'), $this->controller); }); }); return $posts; }
protected function loadPost() { $slug = $this->property('slug'); $post = BlogPost::isPublished()->where('slug', $slug)->first(); /* * Add a "url" helper attribute for linking to each category */ if ($post && $post->categories->count()) { $post->categories->each(function ($category) { $category->setUrl($this->categoryPage, $this->controller); }); } return $post; }
protected function loadPost() { // @deprecated remove if year >= 2015 $deprecatedSlug = $this->propertyOrParam('idParam'); $slug = $this->property('slug', $deprecatedSlug); $post = BlogPost::isPublished()->where('slug', '=', $slug)->first(); /* * Add a "url" helper attribute for linking to each category */ if ($post && $post->categories->count()) { $post->categories->each(function ($category) { $category->setUrl($this->categoryPage, $this->controller); }); } return $post; }
/** * Load post and start building query for related posts */ public function onRun() { $post = Post::where('slug', $this->property('slug'))->with('tags')->first(); if (!$post) { return; } $this->tagIds = []; foreach ($post->tags as $tag) { $this->tagIds[] = $tag->id; } if (empty($this->tagIds)) { return; } $this->query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($query) { $query->whereIn('id', $this->tagIds); })->with('tags'); }
private function loadRelatedPost($numberOfPost) { $post = Post::isPublished()->where('slug', $this->property('slug'))->with('categories')->firstOrFail(); if ($this->property('filter') == 'tag') { $posts = $post->tags()->first()->posts()->isPublished()->with('categories')->orderBy('published_at', 'desc')->take($numberOfPost)->get(); } else { $posts = $post->categories()->first()->posts()->isPublished()->with('categories')->orderBy('published_at', 'desc')->take($numberOfPost)->get(); } /* * Add a "url" helper attribute for linking to each post and category */ $posts->each(function ($post) { $post->setUrl($this->property('postPage'), $this->controller); $post->categories->each(function ($category) { $category->setUrl($this->property('categoryPage'), $this->controller); }); }); return $posts; }
/** * Fetch blog posts * * @return \Illuminate\Database\Eloquent\Collection */ public function index() { $query = Post::isPublished()->select(Settings::get('index_select', ['id', 'slug', 'title', 'excerpt', 'published_at']))->orderBy(Settings::get('index_sort_key', 'published_at'), Settings::get('index_sort_order', 'desc')); // Select custom columns if ($custom = Settings::get('index_select_custom', false)) { $columns = explode(',', $custom); foreach ($columns as $column) { $query->addSelect($column); } } // Search blog posts if ($search = input('search')) { $query->where(function ($q) use($search) { $q->where('title', 'LIKE', '%' . $search . '%')->orWhere('content', 'LIKE', '%' . $search . '%'); // @todo: search blog tags if plugin is installed }); } // Eager load the author information if (Settings::get('index_with_user', false)) { $query->addSelect('user_id')->with(['user' => function ($user) { $user->select('id', 'first_name', 'last_name', 'login', 'email'); }]); } // Eager load images if (Settings::get('index_with_featured_images', false)) { $query->with(['featured_images' => function ($image) { $image->select('attachment_id', 'disk_name', 'file_name', 'title', 'description'); }]); } // Paginate the results if ($perPage = Settings::get('index_per_page', 10)) { $currentPage = input('page') ?: 1; $query->skip($perPage * ($currentPage - 1))->take($perPage); } // Query caching if ($cache = Settings::get('index_cache', false)) { $query->remember($cache); } return $query->get(); }
/** * Load the most related posts based on associated tags * * @return Collection */ protected function loadRelatedPosts() { $post = Post::with('tags')->whereSlug($this->property('slug'))->first(); if (!$post->tags->count()) { return; } $tagIds = $post->tags->lists('id'); // a collection of related posts $query = Post::isPublished()->where('id', '<>', $post->id)->whereHas('tags', function ($tag) use($tagIds) { $tag->whereIn('id', $tagIds); })->with('tags'); $orderBy = DB::raw('( select count(*) from `rahman_blogtags_posts_tags` where `rahman_blogtags_posts_tags`.`post_id` = `rainlab_blog_posts`.`id` and `rahman_blogtags_posts_tags`.`tag_id` in (' . implode(', ', $tagIds) . '))'); // order by most related tags $query->orderby($orderBy, 'desc'); if ($take = intVal($this->property('results'))) { $query->take($take); } return $query->get(); }
protected function loadPost() { $slug = $this->property('slug'); $post = BlogPost::isPublished()->where('slug', $slug)->first(); return $post; }
/** * Get all posts with matching title or content. * * @return Collection */ protected function posts() { return Post::isPublished()->with(['featured_images'])->where(function ($query) { $query->where('title', 'like', "%{$this->query}%")->orWhere('content', 'like', "%{$this->query}%")->orWhere('excerpt', 'like', "%{$this->query}%"); })->orderBy('published_at', 'desc')->get(); }
public function onStart() { $this['posts'] = Post::isPublished()->orderBy('published_at', 'desc')->take(5)->with('categories')->get(); }
public function onLoadMorePosts() { $postsCount = (int) post('postsCount'); $this['posts'] = Post::isPublished()->orderBy('published_at', 'desc')->skip($postsCount * 5)->take(5)->with('categories')->get(); }