Home > Enterprise >  Call Auth::user()->id triggered an error in conditional instruction
Call Auth::user()->id triggered an error in conditional instruction

Time:11-23

I have a simple problem. For example sometimes I need to call Auth::user()->id in conditional but if I'm not loggin it's triggered an error. For example @if(isset(Auth::user()->id)) //do something @endif How can I in simple way make conditional which will be works?

I want to make only one conditional instruction which will be works

CodePudding user response:

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())

CodePudding user response:

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().

  • Related