Attempt to read property "avatar" on null in Laravel

Updated at 08-Mar-2022, By samar

Attempt to read property "avatar" on null in Laravel

Good day, guys. In this post, we’ll look at how to solve the "Attempt to read property "avatar" on null in Laravel" programming puzzle.

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.
  • 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'));
    

Back to code snippet queries related laravel