使用 Gate auth 检查用户是否是版主 - Illuminate\Auth\AuthServiceProvider 缺少参数 3
Using Gate auth to check if a user is a moderator - getting Missing argument 3 for Illuminate\Auth\AuthServiceProvider
我使用 Gate
外观来检查用户是否是 post 所有者或 subreddit 的创建者,然后再允许他编辑甚至查看编辑 link查看。
但是,我无法检查用户是否是给定 subreddit 的版主。已登录用户已被放置为该 subreddit 的版主,但每当我访问它时,我都会收到以下错误
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)
这是我的表格
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...
这是 AuthServiceProvider 中的 Gate 实现
$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;
});
然后在 post.blade.php
上,我在向他显示 html 标记之前检查用户是否具有访问权限,这就是分页符的位置。
@can('update-post', $post, $subreddit)
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endcan
您要发送多个参数,因此您需要将它们分组到一个数组中
@can('update-post', [$post, $subreddit])
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endcan
@halnex
我知道现在开始讨论有点晚了,但是你需要提供 blade 模板以及你用来设置 $gate-> 权限的任何附加变量(除了用户)定义。本质上,select 您所指的 $post 和 $subreddit。 Gate 合约没有 "know" 那些模型,只有 $user 一个。
我使用 Gate
外观来检查用户是否是 post 所有者或 subreddit 的创建者,然后再允许他编辑甚至查看编辑 link查看。
但是,我无法检查用户是否是给定 subreddit 的版主。已登录用户已被放置为该 subreddit 的版主,但每当我访问它时,我都会收到以下错误
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)
这是我的表格
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...
这是 AuthServiceProvider 中的 Gate 实现
$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;
});
然后在 post.blade.php
上,我在向他显示 html 标记之前检查用户是否具有访问权限,这就是分页符的位置。
@can('update-post', $post, $subreddit)
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endcan
您要发送多个参数,因此您需要将它们分组到一个数组中
@can('update-post', [$post, $subreddit])
<a href="{{ action('PostsController@edit', [$post->id]) }}">Edit</a>
@endcan
@halnex
我知道现在开始讨论有点晚了,但是你需要提供 blade 模板以及你用来设置 $gate-> 权限的任何附加变量(除了用户)定义。本质上,select 您所指的 $post 和 $subreddit。 Gate 合约没有 "know" 那些模型,只有 $user 一个。