laravel how to get use with relation in blade after login

  laravel, php

I am using Laravel 8 and the User model has relation with model UserProfile
as following

class User extends Authenticatable
{
    use LaratrustUserTrait;
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;
.
.
.
public function profile(){
        return $this->hasOne(UserProfile::class,'user_id');
    }
}

when i create a new user i directly do login for him and redirect him to dashboard
as following

 Auth::login($user, true);

return $this->sendSuccessResponse();

but my question is how i can use user data with it’s userProfile relation in blade or controller
I tried to use the following code in blade

<img src="{{Auth::user()->profile->image}}" id="profile-img" alt="">

but i get the following error message

Attempt to read property "image" on null

Source: Laravel

Leave a Reply