skip to Main Content

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


  1. You can use the method id() directly, it will return either null or the id of the user

    @if(Auth::id())
    

    Or use check(), it will return true or false and is equivalent for ! is_null(Auth::user())

    @if(Auth::check())
    
    Login or Signup to reply.
  2. You can use Auth::check() for the condition. If you are using guard other than web authentication guard then write like this Auth::guard('guard_name')->check().

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search