i have a class to create my navigation and i use spatie permissions. Now i want to build the navigation with the help of my defined routes. Is it possible to check if the User can access a route before to build a navigation?
i have a class to create my navigation and i use spatie permissions. Now i want to build the navigation with the help of my defined routes. Is it possible to check if the User can access a route before to build a navigation?
Yes you can able to check permission of users. Below are the methods available in spatie permissions.
$user->hasPermissionTo('edit articles');
You can check if a user has Any of an array of permissions:
$user->hasAnyPermission(['edit articles', 'publish articles', 'unpublish articles']);
...or if a user has All of an array of permissions:
$user->hasAllPermissions(['edit articles', 'publish articles', 'unpublish articles']);
You can refer this link
$user is basically your auth()->user()
I am using Laravel 8.x version.
We can check the given permission in controller file eg:
if(Auth::user()->can('product_view')){
// allowed statement will be here
}
while given permission can be checked in view blade file eg:
@can('product_view')
// allowed statement will be here
@endcan
We can also check permission in routes/web.php file:
Route::get('/products/productdetail', [ProductsController::class, 'productdetail'])->name('products.productdetail')->middleware('can:product_view');
Thanks a lot for asking this question. Happy to help :)