I have a simple problem. For example sometimes I need to call Auth::user()->id
in conditional but if I’m not logged in, it triggers an error. For example
@if(isset(Auth::user()->id)) //do something @endif
How can I do that in a simple way to make it work?
I want to make only one conditional instruction that works
2
Answers
You can use the method
id()
directly, it will return eithernull
or the id of the userOr use
check()
, it will returntrue
orfalse
and is equivalent for! is_null(Auth::user())
You can use
Auth::check()
for the condition. If you are using guard other thanweb
authentication guard then write like thisAuth::guard('guard_name')->check()
.