I am new to Laravel. I am now able to register users, log them in. But after login, I don't want to display register and login pages. I want to display logout link. This is easy for me with core php. I want something similar like the one below.

<?php
  if(isset($_SESSION['id'])){
      ?>
      <a href="index.php">Home</a>
      <a href="dashboard.php">Dashboard</a>
      <a href="logout.php">Logout</a>
      <?php
  }else{
      ?>
      <a href="index.php">Home</a>
      <a href="register.php">Register</a>
      <a href="login">Login</a>   
      <?php
  }
  ?>

Thanks for helping

Dani AI

Generated

As showed with plain PHP, the goal is simply: detect an authenticated user and render different nav links. is right that Laravel's auth system is the tool to use — the more idiomatic Blade directives are @guest and @auth. Put the conditional markup inside your main layout (for example resources/views/layouts/app.blade.php) so every page uses the same nav.

Here is a compact, practical pattern to copy into your layout:

<nav>
  <a href="{{ route('home') }}">Home</a>

  @guest
    <a href="{{ route('register') }}">Register</a>
    <a href="{{ route('login') }}">Login</a>
  @else
    <a href="{{ route('dashboard') }}">Dashboard</a>

    <form id="logout-form" action="{{ route('logout') }}" method="POST" style="display:inline">
      @csrf
      <button type="submit" style="background:none;border:none;padding:0;cursor:pointer;">Logout</button>
    </form>
  @endguest
</nav>

Important notes and quick troubleshooting:

  • Logout should be a POST (Laravel protects against CSRF); the submit-button form above is the simplest correct option. If you prefer a link appearance, submit that form from an anchor via JS: onclick="event.preventDefault(); document.getElementById('logout-form').submit();"
  • Protect the dashboard (and other pages) with the auth middleware in routes/web.php so only signed-in users can reach them:
Route::middleware('auth')->group(function () {
  Route::get('/dashboard', [App\Http\Controllers\DashboardController::class, 'index'])->name('dashboard');
});
  • If the nav still behaves oddly, check that your routes exist (php artisan route:list), that the web middleware (session) is active, and you are using the correct guard (e.g. @auth('web') for the web guard). Clearing caches (php artisan view:clear, route:clear) often fixes stale behavior.

Recommended Answers

All 2 Replies

You can do this in Laravel using the auth() helper:

@if (auth()->check())
    <a href="dashboard.php">Dashboard</a>
    <a href="logout.php">Logout</a>
@else
    <a href="register.php">Register</a>
    <a href="login">Login</a>   
@endif

Hey, you can refer to this Source. You can find more about laravel here. Can be helpful!

You can do this in Laravel using the auth() helper:

@if (auth()->check())
    <a href="dashboard.php">Dashboard</a>
    <a href="logout.php">Logout</a>
@else
    <a href="register.php">Register</a>
    <a href="login">Login</a>   
@endif

Hey, you can refer to this Source. You can find more about laravel here. Can be helpful!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.