Laravel 5: Change navbar if user is logged

bockzior picture bockzior · Mar 29, 2015 · Viewed 56.5k times · Source

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!

Answer

Jacob picture Jacob · Mar 29, 2015

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