public function search(Post $post, Subreddit $subreddit, Request $request)
 {
     $query = $request->input('search');
     $subreddit = Subreddit::with('posts.votes')->with('moderators.user')->first();
     $posts = Post::where('title', 'LIKE', '%' . $query . '%')->get();
     $isModerator = false;
     return view('site.search', compact('query', 'subreddit', 'posts', 'isModerator'));
 }
 public function create(Subreddit $subreddit, User $user)
 {
     if (Gate::denies('update-sub', $subreddit)) {
         Session::flash('message_danger', 'You are not allowed to do that.');
         Session::flash('alert-class', 'alert-danger');
         return redirect('mysubreddits');
     } else {
         $subreddit = Subreddit::with('user')->findOrFail($subreddit->id);
         $moderators = Moderator::where('subreddit_id', '=', $subreddit->id)->get();
         return view('subreddit/moderators/create')->with(compact('subreddit', 'moderators'));
     }
 }
 /**
  * Display the specified resource.
  *
  * @internal param Subreddit $subreddit
  */
 public function show(Subreddit $subreddit)
 {
     /*$subreddit = Subreddit::findOrFail($id);
             $subreddit->load('posts');
     
             if(is_null($subreddit)) {
                 abort(404);
             }*/
     /*$posts = DB::table('subreddits')
       ->join('posts', 'subreddits.id', '=', 'posts.subreddit_id')
       ->select('posts.*')
       ->take(5)
       ->get();*/
     $posts = Subreddit::findOrFail($subreddit->id)->posts()->get();
     return view('subreddit/show')->with('subreddit', $subreddit)->with('posts', $posts);
 }
 public function search(Post $post, Request $request)
 {
     $query = $request->input('search');
     $subredditId = $request->input('subreddit_id');
     $subreddit = Subreddit::with('posts.votes')->with('moderators.user')->where('id', $subredditId)->first();
     $posts = $subreddit->posts()->where('title', 'LIKE', '%' . $query . '%')->get();
     $isModerator = $subreddit->moderators()->where('user_id', Auth::id())->exists();
     $modList = Moderator::where('subreddit_id', '=', $subredditId)->get();
     return view('subreddit.search', compact('query', 'subreddit', 'posts', 'isModerator', 'modList'));
 }
 public function getSubredditMetaData($subreddit)
 {
     $sub = Subreddit::whereName($subreddit)->firstOrFail();
     $data = ["subscriber_count" => count($sub->users), "post_count" => count($sub->posts), "description" => $sub->description];
     return response()->json($data);
 }
 /**
  * Display the specified resource.
  *
  * @internal param Subreddit $subreddit
  * @param Subreddit $subreddit
  * @return $this
  */
 public function show(Subreddit $subreddit)
 {
     $subreddit = Subreddit::with('posts.votes')->findOrFail($subreddit->id);
     return view('subreddit/show')->with('subreddit', $subreddit);
 }
 public function showPost($subreddit, $permalink, $slug)
 {
     $sub = Subreddit::whereName($subreddit)->firstOrFail();
     $post = $sub->posts()->wherePermalink($permalink)->whereSlug($slug)->firstOrFail();
     return view('subreddit.showPost', compact('post'));
 }