我正在尝试创建一个自定义门,允许“管理员”团队的用户访问用户索引页面。但是,它的功能与我想要实现的完全相反,我似乎不明白我在哪里错了。
无论用户是否属于管理团队,它总是返回“false”。
帮助表示赞赏。谢谢你。
用户.php:
public function AdminTeam(string $team)
{
return null !== $this->teams()->where('name', $team)->first();
}
AuthServiceProvider.php :
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
$this->registerPolicies();
Gate::define('is-admin', function ($user){
return $user->AdminTeam('Administrator');
});
}
用户控制器.php:
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (Gate::allows('is-admin')) {
return view('admin.users.index', ['users' => User::paginate(10)]);
}
dd('you need to be admin!');
}
index.blade.php :
@can('is-admin')
@foreach($users as $user)
<tr>
<th scope="row">{{ $user->user_id }}</th>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td>{{ $user->created_at }}</td>
<td>{{ $user->updated_at }}</td>
<td>
<a class="btn btn-sm btn-primary" href="{{ route('admin.users.edit', $user->user_id) }}"
role="button">Bearbeiten</a>
<button type="button" class="btn btn-sm btn-danger"
onclick="event.preventDefault();
document.getElementById('delete-user-form-{{ $user->user_id }}').submit()">
Löschen
</button>
<form id="delete-user-form-{{ $user->user_id }}"
action="{{ route('admin.users.destroy', $user->user_id) }}" method="POST"
style="display: none">
@csrf
@method("DELETE")
</form>
</td>
</tr>
@endforeach
@endcan
输出 :