Attempt to read property "avatar" on null in Laravel

Created 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

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.