If statement 如何使用laravel Spatial软件包检查if语句中的多个权限条件?
我需要在if语句中检查多个权限的条件。它使用的是来自laravel的Spatial软件包。我使用下面的代码,但它似乎不起作用。它可以显示输出,但输出不正确。它不会过滤条件If statement 如何使用laravel Spatial软件包检查if语句中的多个权限条件?,if-statement,laravel-5,spatie,If Statement,Laravel 5,Spatie,我需要在if语句中检查多个权限的条件。它使用的是来自laravel的Spatial软件包。我使用下面的代码,但它似乎不起作用。它可以显示输出,但输出不正确。它不会过滤条件 if (auth()->user()->can('View Consultant') || auth()->user()('View Administration') || auth()->user()('View Accountant') || auth()->user()('All depa
if (auth()->user()->can('View Consultant') || auth()->user()('View Administration')
|| auth()->user()('View Accountant') || auth()->user()('All departments'))
{
$itemregistrations = DB::table('itemregistrations')
->where('categoryid','=', '1',',','2',',','3')
->get();
return view('profil.index', compact('itemregistrations'));
}
if (auth()->user()->hasAnyPermission(['View Consultant', 'View Administration', 'View Accountant', 'All departments'])
{
$itemregistrations = DB::table('itemregistrations')
->where('categoryid','=', '1',',','2',',','3')
->get();
return view('profil.index', compact('itemregistrations'));
}
代码正确吗
条件是具有权限的用户(查看顾问、查看管理、查看会计、所有部门)可以查看所有部门的顾问、管理和会计列表
对于具有权限的用户(仅查看顾问),只能查看顾问列表。根据文档 您可以检查用户是否具有以下权限数组: 因此,您可以执行以下操作来检查多个条件
if (auth()->user()->can('View Consultant') || auth()->user()('View Administration')
|| auth()->user()('View Accountant') || auth()->user()('All departments'))
{
$itemregistrations = DB::table('itemregistrations')
->where('categoryid','=', '1',',','2',',','3')
->get();
return view('profil.index', compact('itemregistrations'));
}
if (auth()->user()->hasAnyPermission(['View Consultant', 'View Administration', 'View Accountant', 'All departments'])
{
$itemregistrations = DB::table('itemregistrations')
->where('categoryid','=', '1',',','2',',','3')
->get();
return view('profil.index', compact('itemregistrations'));
}
如果需要检查模型是否具有所有权限,则应使用方法
hasAllPermissions()
。例如:
if(\Auth::user()->hasAllPermissions('View Consultant','View Administration','View accountary','All departments')){
//做点什么
}
可通过两种方式使用。
单个控件如下所示
@can('edit articles')
//
@endcan
@if(auth()->user()->can('edit articles') && auth()->user()->can('edit uploads'))
//
@endif
多重控制如下所示
@can('edit articles')
//
@endcan
@if(auth()->user()->can('edit articles') && auth()->user()->can('edit uploads'))
//
@endif
我正在使用(Auth::user()->hasPermissionTo('View Consultant')可以解决if语句的问题..谢谢你的回复..是的,这也行。我是根据你的问题代码回答的。