user2301515
user2301515

Reputation: 5107

Hiding redudant link in Laravel 8

I want to hide link

@if(Route::has('dashboard.index'))
<li class="nav-item">
    <a class="nav-link" aria-current="page" href="{{ route('dashboard.index') }}">{{__('My Admin')}}</a>
</li>
@endif

And if I logged out and in web.php ,i hide route with middlewares (e.g authsanctum and verified):

Route::middleware(['auth:sanctum', 'verified'])->get(['DashContro::class','index])->name('dashboard.index');

.. but the Route::has() is still 'true' What to use instead of route has for hiding links while logged out?

Upvotes: 0

Views: 68

Answers (2)

ceejayoz
ceejayoz

Reputation: 180004

Route::has checks if the route exists, not if it's accessible to the user.

Instead, you should use Blade's @auth and @guest directives to show different things for logged-in or logged-out users.

https://laravel.com/docs/8.x/blade#authentication-directives

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

@guest
    // The user is not authenticated...
@endguest

Upvotes: 1

D1__1
D1__1

Reputation: 1357

You can conditionally load HTML in blade based on login with the code below. The function Auth::check returns true if the user is authenticated.

@if(Auth::check())
    // only show this if someone is logged in.
@endif

Upvotes: 1

Related Questions