Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Blogpost;
- use App\User;
- use App\Comment;
- use App\Category;
- class BlogpostController extends Controller
- {
- public function overview()
- {
- $blogposts = Blogpost::orderBy('created_at', 'asc')->get();
- return view('blog.overview', [
- 'blogposts' => $blogposts
- ]);
- }
- public function detail($id)
- {
- $blogpost = Blogpost::findOrFail($id);
- return view('blog.detail', [
- 'blogpost' => $blogpost
- ]);
- }
- public function comments($id)
- {
- $blogpost = Blogpost::findOrFail($id);
- $comments = $blogpost->comments; // can be applied in Blade directly (!)
- return view('blog.comments', [
- 'blogpost' => $blogpost,
- 'comments' => $comments
- ]);
- }
- public function search(Request $request){
- //get params
- $terms = $request->terms;
- $from = $request->from;
- $to = $request->to;
- $category_id = $request->category_id;
- //get categories
- $categories = Category::pluck('name','id');
- //add empty category
- $categories->prepend('select category');
- //$blogposts = Blogpost::orderBy('created_at', 'asc');
- $blogposts = Blogpost::orderBy('created_at', 'asc');
- if(!empty($terms)){
- $termArray = explode(' ', $terms);
- foreach($termArray as $term) {
- //$blogposts->where('title',$term)->orWhere('title','like','%',$term,'%');
- $blogposts->where(function($query) use ($term){
- $query->orWhere('content','like','%'.$term.'%')->orWhere('title','like','%'.$term.'%');
- });
- }
- }
- //get posts between the set dates
- if(!empty($from) && !empty($to)){
- $blogposts = $blogposts->whereBetween('created_at',array($from,$to));
- }
- //get posts with set category id if set
- if(!empty($category_id) && $category_id > 0){
- $blogposts = $blogposts->where('category_id',$category_id);
- }
- $blogposts = $blogposts->get();
- return view('blog.search', [
- 'blogposts' => $blogposts,
- 'categories' => $categories,
- 'terms' => $terms,
- 'from' => $from,
- 'to' => $to
- ]);
- }
- public function getResults($id){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement