title: Laravel 5: check if user belongs to the moderators list before allowing him to edit
body: <p>I am using <code>Gate</code> authorization to check if the user has permissions to access or see something.</p>
  
  <p>For example I have a <code>update-post</code> gate definition that checks if the user owns the post or owns the sub, if yes, he will be able to see the "Edit" link and access the Edit route. If not, he won't be able to see anything.</p>
  
  <p>I am now trying to add another permissions to check if the user belongs to the "moderators" table, if yes, he will also be able to see the "Edit" link on <code>subreddit/show</code></p>
  
  <p>It works great for the first 2 checks, but fails on the third and the "Edit" link is always visible now, even though I cannot access the edit route of posts that aren't mine or belong to the subreddit I own.</p>
  
  <p><strong>EDIT:</strong> I have edited <code>$moderators_list</code> it now check if the logged in user is a moderator of the subreddit that the post he's viewing. <code>dd($moderators_list</code> gets me a list with correct values. But I can't use <code>$moderators_list-&gt;user_id</code> that will give <code>Undefined property: Illuminate\Database\Eloquent\Collection::$user_id</code> </p>
  
  <p><strong>EDIT 2:</strong> I have fixed the query that gets the user_id of the user who is a moderator in a subreddit. So now using <code>$moderators_list-&gt;user-id</code> gets me the id of 3 which is the user id of the current logged in. But the gate checking still fails, unable to edit posts in the subreddit I am moderator in.</p>
  
  <p>My tables</p>
  
  <pre><code>users: id, name, email...
  subreddits: id, user_id...
  posts: id, user_id, subreddit_id...
  moderators: id, user_id, subreddit_id...
  </code></pre>
  
  <p>This is the <code>$gate</code> definitions</p>
  
  <pre><code>$gate-&gt;define('update-post', function ($user, $post, $moderators_list) {
          // Check if user is subreddit owner
          if ($user-&gt;id === $post-&gt;subreddit-&gt;user-&gt;id) {
              return true;
          }
  
          // Check if user is the post author
          if ($user-&gt;id === $post-&gt;user_id) {
              return true;
          }
  
          // Check if user is a moderator of a subreddit
          if ($user-&gt;id === $moderators_list-&gt;user_id) {
              return true;
          }
  
          return false;
  });
  </code></pre>
  
  <p>And this is <code>show()</code> method on <code>PostsController</code></p>
  
  <p>Please note that <code>dd($moderators_list)</code> will output a list of all moderators of the subreddit this post is located in, so I am getting the correct result.</p>
  
  <pre><code>public function show(Post $post, Subreddit $subreddit, Moderator $moderator)
  {
          $post = Post::with('user.votes')-&gt;findOrFail($post-&gt;id);
          $moderator = Moderator::where('user_id', '=', Auth::id())-&gt;first();
          $moderators_list = Moderator::where('subreddit_id', '=', $post-&gt;subreddit-&gt;id)-&gt;where('user_id', '=', Auth::id())-&gt;first();
  
          return view('post/show')-&gt;with('post', $post)
                                  -&gt;with('moderator', $moderator)
                                  -&gt;with('moderators_list', $moderators_list);
  }
  </code></pre>
  
  <p>And this is how I check to see if user has access to see the "Edit" link on the view</p>
  
  <pre><code>@can('update-post', [$post, $moderator, $moderators_list])
       &lt;a href="{{ action('PostsController@edit', $post-&gt;id) }}"&gt;Edit&lt;/a&gt;
  @endcan
  </code></pre>