Osama Amr
Osama Amr

Reputation: 149

how to check the middleware in blade view

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'));
}

Upvotes: 0

Views: 754

Answers (2)

Maik Lowrey
Maik Lowrey

Reputation: 17566

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

``

Upvotes: 1

raghav
raghav

Reputation: 96

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

Upvotes: 1

Related Questions