Home > Blockchain >  How to get the authenticated user avatar in the header blade?
How to get the authenticated user avatar in the header blade?

Time:11-30

in profile page a user has an option to choose an avatar picture, and in the header.blade.php I want to check if the user has chosen an avatar ,if yes, then display the avatar picture next to the user name. now in profile.blade.php I can show the chosen avatar with this line of code

<img style="border-radius: 50%;" src="{{url('/')}}/Uploads/avatars/{{$myAvatar->image}}" height="60px;" width="60px;" alt="" id="avatar-preload" name="avatar-preload">

in profile controller i have index function which passes $myAvatar to profile.blade.php

public function index()
    {
        $user= User::where('id', Auth::user()->id)->first();
        $myAvatar = Avatar::find($user->avatar_id);
        return view('admin.profile.index')
               ->with('avatars', Avatar::all())
               ->with('myAvatar',$myAvatar);
    }

I want to apply the same logic to bring the avatar picture to header.blade.php but I dont have a controller to send data to the the header blade through it, how can I do that?

Every user now has an avatar_id which refers to the id of the chosen avatar in the avatar table

CodePudding user response:

In your User model you could define a relationship with Avatar

public function avatar()
{
    return $this->hasOne(Avatar::class, 'id','avatar_id');
}

Then in your blade, you could use Auth::user()->avatar anywhere you want.

CodePudding user response:

Create A Model Relationship

public function getAvtar()
{

    return Avatar::where('your filed name',Auth::user()->id);

}

Then Call Like This

Auth::user()->getAvtar()
  • Related