J. Robinson J. Robinson - 1 month ago 19
PHP Question

Laravel Blade If Statement throwing Non-Object Property Error upon Auth Check with Guard

I'm VERY new to laravel, so I'm honestly not sure what information to provide but I'll try to the best of my ability to ask the question throughly.

Lets take a look at my code:

@if(Auth::guard('admin')->check() || Auth::guard('web')->check())
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
{{ Auth::user()->name }} <span class="caret"></span>
</a>


{{-- Authenticated Dropdown Menu --}}
<ul class="dropdown-menu" role="menu">
{{-- Admin Links --}}
@if (Auth::guard('admin')->check())
<li><a href="{{ route('posts.index') }}">Manage Blog Posts</a></li>
<li role="separator" class="divider"></li>
@endif

{{-- User Links --}}
@if (Auth::guard('web')->check() || Auth::guard('admin')->check())
<li><a href="#">User Test Link</a></li>
<li role="separator" class="divider"></li>
@endif

{{-- Logout and Form --}}
<li>
<a href="{{ route('logout') }}"
onclick="event.preventDefault();
document.getElementById('logout-form').submit();">
Logout
</a>
{!! Form::open(['route' => 'logout', 'id' => 'logout-form', 'style' => 'display: none;']) !!}
{!! Form::close() !!}
</li>
{{-- End Logout Form --}}
</ul>
</li>

@else
<li><a href="{{ route('login') }}">Login</a></li>
<li><a href="{{ route('register') }}">Register</a></li>
@endif


Now I thought that
Auth::guard('admin')->check()
returned true or false depending on if the user has authentication or not. I also need to run this if statement against two guards, and checking if the admin is logged in OR the user is logged in.

Basically, as you can probably tell from my code, i want the bootstrap dropdown to display if logged in, if not then display login and register buttons.

THEN If the
User
is logged in the dropdown won't show the "Manage Blog Posts" link because thats an
Admin
link... If an
Admin
is logged in however then display both links.

As you can see in this snippet below, this is the SAME EXACT if statement as the very top. This one works just fine, but the parent if statement doesn't want to seem to work.

{{-- User Links --}}
@if (Auth::guard('web')->check() || Auth::guard('admin')->check())
<li><a href="#">User Test Link</a></li>
<li role="separator" class="divider"></li>
@endif


This error only seems to happen when I'm logged in as an admin ONLY and I try to visit any page thats not protected by my
auth:admin
middleware.

Am I getting this error because I'm nesting my if statements incorrectly? Maybe I'm just tired and need a second set of eyes to see the smallest error. I've followed DevMarketers Multi Auth tutorial on YouTube to help set this up.

(DevMarketer's YouTube Multi-Auth Tutorial: https://youtu.be/iKRLrJXNN4M?list=PLwAKR305CRO9S6KVHMJYqZpjPzGPWuQ7Q)

Attached is a screenshot of the error message(s) I'm getting as well as a pastebin for the entire _nav.blade.php. This view is a partial. I did also just run composer update before i started working and noticed that the laravel framework just went from 5.4.32 to 5.4.33. Don't know if that would make a dramatic difference or not.

https://pastebin.com/pLDTmU4w

http://imgur.com/a/0y6kd

I can provide my github repo link for this project upon request.

Answer Source

Because you are using the admin guard you need to retrieve the user from that. Try this on line 38:

{{ Auth::guard('admin')->user()->name }}

or you could check if admin

{{ Auth::guard('admin')->check() ? Auth::guard('admin')->user()->name : Auth::user()->name }}