skip to Main Content

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
  }
?>

3

Answers


  1. Blade has @auth, which checks if user is logged in or not

    @auth
    /* what logged in users should see */
    @else
    /* what guests should see */
    @endauth
    

    Also has @guest (ending with @endguest) which does the opposite.

    And if you named your routes, you can for example:

    href="{{route('login')}}" 
    

    This way the url changes if you change your routes.

    Login or Signup to reply.
  2. @if(Auth::user())
    <button>I'm logged in</button>
    @else
    <button>I'm not logged in </button>
    @endif
    
    Login or Signup to reply.
  3. This is How I did it. It’s working though, but I don’t know if it’s the right way.

     @if(!Session::has('users')
       <a href="">Home</a>
       <a href="">Register</a>
       <a href="">Login</a>
     @elseif(Session::has('users')
       <a href="">Home</a>
       <a href="">Dasboard</a>
       <a href="">Logout</a>
     @ndif
    

    ‘users’ is the user id as the Session variable from database

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search