1

I'm trying to create a dynamic navigation bar on my Laravel project, that, if the user is logged in, outputs Hello, {{$user->name}}!.

This, however, is not working.

If I try to add that $user->name variable to my navbar layout I simply get the following error: Undefined variable: user (View: C:\xampp\htdocs\laravel\app\views\layout\navbar.blade.php).

This layout is included in the main layout like so: @include('layout.navbar') and the code is as follows:

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" id="logo" href="{{URL::route('home')}}">asd</a>
            <ul class="nav navbar-nav">
                @if(Auth::check())
                <li>
                    <a>Hello, {{$user->name}}!</a>
                </li>
                @endif
            </ul>
        </div>
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                @if(Auth::check())
                <li>
                    <a href="{{URL::route('profile', $user->name)}}">Profile</a>
                </li>
                <li>
                    <a href="{{URL::route('account-logout')}}">Logout</a>
                </li>                
                @else
                <li>
                    <a href="{{URL::route('account-login')}}">Login</a>
                </li>
                <li>
                    <a href="{{URL::route('account-create')}}">Register</a>
                </li>                
                @endif
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>

What is the correct way to access a user object (with the information of the user that is currently logged in) within this context?

2 Answers 2

3

if your trying to access logged user data like,

$user->name

then you need to pass the logged user in to the blade view from the controller as,

retun View::make("pathAndNameOfBladeFile")->with(Array("user" => Auth::user()));

and change the @if(Auth::check()) in to @if($user) in the blade view

-- OR

in the top of the blade file add this, (put this before using the $user in your code)

@if(Auth::check())
    <?php $user = Auth::user(); ?>
@endif

-- OR

you can do like something @karrde00 suggested in the answer.

The cleaner why to do this is pass the $user from the controller.

Sign up to request clarification or add additional context in comments.

Comments

1

You are going to want to use something like {{ Auth::user()->name }} in Laravel. It will give you the logged in user for the view in a Blade template.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.