Laravel 5: Change navbar if user is logged
Asked Answered
C

8

48

I'm completely new to Laravel, MVC and templating engines in general.

I need to show certain navbar buttons and options if a user is logged in such as: Notifications, Logout, Profile, etc... and a Login button otherwise.

Any help on how I could address this the right way is greatly appreciated. This is what I'm considering at the moment:

  • A User object is always passed to the view.
  • The view checks if the User is set (meaning it's logged in) to include the appropriate partial blade template for the navbar.

app.blade.php:

...
@if (isset($user))
     @include('partials.navbarlogged')

@else
     @include('partials.navbar')
...

Is this the best method? Thanks for your time!

Carranza answered 29/3, 2015 at 18:43 Comment(3)
Did you create the User model yourself or are you using Laravel's built in User model?Rotorua
The answer to this question should appropriately answer yours too.Campaign
@guest ... @endguestScreen
R
88

If you are using Laravel 5's built in User model you can simply do

@if (Auth::check())
  //show logged in navbar
@else
  //show logged out navbar
@endif
Rotorua answered 29/3, 2015 at 18:47 Comment(4)
I'm not using the default Auth mechanisms, so that won't work, will need to find a way to get it to work with my custom OpenID authentication. But it looks like I'm in the good direction in terms of templating. Thanks for your time!Carranza
I would avoid passing the entire User object around, so maybe a small auth helper is in order? Create your own helper file so you can do all of the messy code in there, so in the template you could do something like Auth::check()Rotorua
What would be the recommended location for that new class? In fact the entire User object isn't needed. Would it be reasonable to store the needed fields from it in the Session (name, image_url, email, ...) and read from the Session directly in the view? Thanks once again.Carranza
Storing specific values in the session is preferable to storing the entire object, but preferably you would not need to pass the User object or any values of it unless you need to specifically access them. Create an auth facade to handle authentication. The documentation instructs on how to create a facade: laravel.com/docs/5.0/facadesRotorua
O
55

For laravel 5.7 and above, use @auth and @guest directives.Here is the official documentation here

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

You may specify the authentication guard that should be checked when using the @auth and @guest directives:

@auth('admin')
    // The user is authenticated...
@endauth

@guest('admin')
    // The user is not authenticated...
@endguest

Otherwise, you can use the @unless directive:

@unless (Auth::check())
    You are not signed in.
@endunless
Opening answered 13/11, 2018 at 13:9 Comment(0)
B
30

New versions of Laravel (5.6 at time of writing) support these two directives in Blade:

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

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

See official documentation here: https://laravel.com/docs/5.6/blade

Bethanybethe answered 8/4, 2018 at 16:26 Comment(0)
S
8

You can also use Auth::guest()

The Auth::guest() method returns true or false.

Example -

@if (Auth::guest())
    <a href="{{ route('login') }}">Login</a>
    <a href="{{ route('register') }}">Register</a>
@else
    {{ Auth::user()->name }}
    <a href="{{ route('logout') }}">Logout</a>
@endif
Ster answered 22/11, 2017 at 16:26 Comment(0)
C
5

This should help :

@extends(Auth::check() ? 'partials.navbarlogged' : 'partials.navbar')

The Auth::check() sees whether the user is logged in or not and returns a boolean.

Campaign answered 18/3, 2019 at 4:56 Comment(0)
D
4

Starting from Laravel 5.4 there's new blade directive @includeWhen which includes view based on a given boolean condition :

@includeWhen(Auth::check(), 'partials.navbarlogged')
@includeWhen(Auth::guest(), 'partials.navbar')
Dinner answered 3/7, 2018 at 13:40 Comment(0)
E
0

If you are using Laravel 5's built-in User model you can simply do:

@if (Auth::check())
   //show logged in navbar
@else
   //show logged out navbar
@endif
Eel answered 16/9, 2021 at 8:40 Comment(0)
S
0

Or you could use the @auth and @guest directives (see docs). An example from the documentation

@auth
    // The user is authenticated...
@endauth
 
@guest
    // The user is not authenticated...
@endguest
Skippie answered 11/5, 2023 at 8:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.