Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

VortixDev's avatar

Auth::guard('guard')->guest() - Trying to get property of non-object

I use Auth::guard('guard')->guest() in the layout of my page to determine whether or not to show the "Login" and "Register" buttons or not - this 'guard' is the guard I have created for a second type of user my system uses. When I'm on a page which uses a controller that utilises the "auth" middleware with parameters "web,guard" then the page loads correctly, but when I visit a page which uses a controller that utilises the "guest" middleware with no parameters then the page doesn't load correctly and gives the error "Trying to get property of non-object". When I (on the same page) have the result of guest() and the result's type printed I get that it is a true boolean (as expected). When I print the type of Auth::guard('guard'), I get that it is an object. I'd appreciate any help understanding this.

0 likes
3 replies
vipin93's avatar

instead of Auth::guard('guard')->guest() use

@if(!Auth::check())

and your question little bit confusing explain in detail

VortixDev's avatar

The following check exists within the layout file:

@if (Auth::guard('web')->guest() and Auth::guard('radius')->guest())

That produces an error when I visit the login page: "Trying to get property of non-object", even though {{ gettype(Auth::guard('radius')) }} and {{ gettype(Auth::guard('radius')->guest()) }} confirm that Auth::guard produces an object and the guest method can be applied to that to produce a boolean. Removing the Auth::guard('radius') check and leaving in the Auth::guard('web') check makes the issue go away.

When, however, I visit the home page, the error doesn't show, even with both conditions in. The main difference between the two pages is the middleware used - "auth" is used by the home controller, whereas "guest" is used by the login page. The home controller uses "auth:web,radius" so that it can redirect any users who aren't logged in with my custom guard as well as the in-built session guard. The login controller uses "guest" alone, redirecting anyone who is logged in using "web" away from the login pages. This means that my "radius" guard is not redirected away. I am wanting to modify the "guest" middleware to make it do so, and this would circumvent my problem, however I don't want to move on until I understand and fix the issue.

To test, I changed the login page to use the "auth" middleware with the parameters I specified for the home controller to use. When I did this, I could use both conditions previously stated without receiving the error.

It seems that check() isn't working because Auth::user() produces null when logged in with my guard and on the login page, but doesn't produce null when logged in with my guard and on the home page.

VortixDev's avatar

Also, removing the condition and using the following:

{{ gettype(Auth::guard('radius')) }} {{ gettype(Auth::guard('radius')->guest()) ? "True" : "False" }}

Gives me "object" and "True" as results.

Please or to participate in this conversation.