Search code examples
phplaravellaravel-5.3

Laravel get session not returning an anything


i am learning laravel, and i am using Auth::login on signup and Auth::attempt on login, i logged in and i am doing the following in my master layout (using blade php)

        @if(!Auth::check())
        <li><a href="#">Home</a></li>
        @endif
        @if(Auth::check())
            <li><a href="{{ route('news') }}">News</a></li>
            <li><a href="{{ route('admin') }}">Admin</a></li>
            <li><a href="#">{{ session()->get('username') }}</a></li>
        @endif

as you probably have noticed this is for the menu, when i am logged out i only get the Home menu and when i am logged in i only get News and Admin, i am trying to set display the users username in the menu aswell which will route to profile (not yet created)

i am sincerely sorry if i made a stupid mistake, im good at php but i am still very new to laravel and MVC in general


Solution

  • Depending on your session management code, your username might not be present in the Session facade. The default way of getting your user's data after login is to use: Auth::user().

    If a user has the public attribute username, you'll be able to get it through

    Auth::user()->username
    

    By the way, it would also be good to use @else instead of checking again for inverse Auth::check() condition