我想为具有“管理员”角色的用户显示一个表单。我怎样才能做到呢?现在我明白了,总是假的。
政策
public function view(User $user)
{
foreach ($user->roles as $role) {
$role->name == 'admin';
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
控制器
public function index()
{
$user = Auth::user();
if($user->can('view', $user))
{
$listSections = Section::all();
return view('sections.index', compact('listSections'));
}
else
{
abort(403, 'Only admins can');
}
}
Run Code Online (Sandbox Code Playgroud)
看法
@can('view')
@include('sections.show')
@endcan
Run Code Online (Sandbox Code Playgroud)
谢谢帮助。