Hossam Thapet
Hossam Thapet

Reputation: 129

Trying to get property 'user' of non-object

in my navbar i want to allow upload link for admin role and download for user role i got this error ... thats my blade.php file

  @if (Route::has('register'))
      @if(auth::user()->user)
          <li class="nav-item">
              <a class="nav-link" href="{{ route('download') }}">{{ __('download') }}</a>
          </li>
      @endif
  @else
      <li class="nav-item ">
          <a  class="nav-link capital" href="{{ route('upload') }}">{{ __('upload') }}</a>
              
          </a>
          
      @endif
      @endif

Upvotes: 0

Views: 84

Answers (3)

Kamlesh Paul
Kamlesh Paul

Reputation: 12391

you can use @auth @endauth

@auth
     <li class="nav-item">
          <a class="nav-link" href="{{ route('download') }}">{{ __('download') }}</a>
     </li>
@endauth

ref link https://laravel.com/docs/8.x/blade#authentication-directives

Upvotes: 1

alernative you can use optional global helper

optional(Auth::user())->$anyProperty;

Upvotes: 0

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

If you user is guest then you have no record on DB, so first check if has been authenticated or not.

change this line:

@if(auth::user()->user)

to this

@if(Auth::check())

Upvotes: 1

Related Questions