Home > other >  how to check the middleware in blade view
how to check the middleware in blade view

Time:10-19

in my navbar I am using @guest and @auth to hide some navbar links from the guests, now I need to show some links to the admin, how to make this?

the Admin middleware

public function handle(Request $request, Closure $next)
{
    if (Auth::check() && Auth::user()->is_admin == 1) {
        return $next($request);
    }

    return redirect(route('home'));
}

CodePudding user response:

This works in all laravel versions:

@if(Auth::check())
    // User is authenticated...
@else
    // User is not authenticated...
@endif

Laravel > 5.5:

@auth
    // user is authenticated...
@endauth

@guest
    // User is not authenticated...
@endguest

``

CodePudding user response:

you can use laravel gate go to App/Providers/AuthServiceProvider.php and write this code inside boot function of provider it make a gate

Gate::define("Admin",function(User $user){
      if($user->is_admin){
         return true;
      }
      return false; 
});

note:- use Illuminate\Support\Facades\Gate; at the top of provider

now your gate is ready:- inside your blade you can check a user is admin or not using @can

@can('Admin')
   "write something which only admin see"
 @endcan
  • Related