Using Gate auth to check if a user is a moderator - getting Missing argument 3 for Illuminate\Auth\AuthServiceProvider
I use the Gate facade to check if a user is the post owner or the creator of a subreddit before allowing him to edit or even view the edit link on the view.
However, I'm having trouble check if a user is a moderator of a given subreddit. The logged in user has already been placed as a moderator for that subreddit but whenever I visit it, I get the following error
ErrorException in AuthServiceProvider.php line 18:
Missing argument 3 for Illuminate\Auth\AuthServiceProvider::Illuminate\Auth{closure}() (View: C:\xampp\htdocs\reddit\resources\views\partials\post.blade.php) (View: C:\xampp\htdocs\reddit\resources\views\partials\post.blade.php)
Here are my tables
users: id, name, email...
posts: id, user_id, subreddit_id...
subreddits: id, name, user_id (to designate the owner of the sub)
moderators: id, user_id, subreddit_id...
Here's the Gate implemention inside AuthServiceProvider
$gate->define('update-post', function ($user, $post, $subreddit) {
// Check if user is subreddit owner
if ($user->id === $post->subreddit->user->id) {
return true;
}
// Check if user is the post author
if ($user->id === $post->user_id) {
return true;
}
// this is where I'm having trouble
if($user->id === $subreddit->moderator->user_id) {
return true;
}
return false;
});
Then on post.blade.php I check if the user has access before showing him the html markup and this is where the page breaks.
@can('update-post', $post, $subreddit)
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endcan
Please or to participate in this conversation.