Attempt to read property "avatar" on null in Laravel

Error Attempt to read property "avatar" on null occurs when you to try to display the user avatar and $user->avatar returns null while displaying user information in blade file.

Answers 1
  • Display user avatar with condition in Laravel blade

    <img src="{{ $user->avatar !== null ? asset($user->avatar) : asset('/')  }}" alt=".."/>
    
    //Controller Code.
    $user = User::query()->where('id','=', $id)->first();
    return view('user.profile', compact('user'));
    
    0
  • Back to code snippet queries related laravel