ene's avatar
Level 2

check Avatar Based On Gender

I want to implement a check when a user have already uploaded avatar don't display default avatar meanwhile default avatar is base on gender. I have 2 image files called "male.png" and "female.png" at public folder

0 likes
15 replies
tykus's avatar

Make an accessor on the User model to sort it out, e.g.something like the following:

public function getAvatarAttribute()
{
    if ($this->attributes['avatar']) {
        return $this->attributes['avatar'];
    }

    return $this->attributes['gender'] === 'm' ? public_path('male.png') : public_path('female.png');
}

This assumes there is a nullable avatar column on the users table containing the path to an uploaded image; otherwise checks the user's gender... adjust according to your actual columns/values

ene's avatar
Level 2

@tykus will I call it like this in the view {{$user->avatar }}

Snapey's avatar

and if they don't identify as either?

tykus's avatar

@Snapey thought about that too, but there were only two images in the public directory 🤷‍♂️

tykus's avatar

@ene you would simply modify the accessor in that case (making a key/value pair for each specified gender and ensuring the image path exists; for each case as well as the fallback:

public function getAvatarAttribute()
{
    if ($this->attributes['avatar']) {
        return $this->attributes['avatar'];
    }

    return [
        'm' => public_path('male.png'),
        'f' => public_path('female.png'),
        // ...
    ][$this->attributes['gender']] ?? public_path('not_specified.png'),
}
ene's avatar
Level 2

@tykus i could not call the avatar correctly

<img class="w-20 h-20 rounded-full" src="{{ $user->avatar }}" alt="">
Sinnbeck's avatar

@ene please explain how it does not work. Error? Wrong image? It's hard to guess the problem when the code is supposed to work

ene's avatar
Level 2

@Sinnbeck the image is not displaying after calling it like this

<img class="w-20 h-20 rounded-full" src="{{ $user->avatar }}" alt="">

the avatar are stored in public/ IMG so I did this


public function getAvatarAttribute()
{
    if ($this->attributes['avatar']) {
        return $this->attributes['avatar'];
    }

    return [
        'm' => public_path('img/male.png'),
        'f' => public_path('img/female.png'),
        // ...
    ][$this->attributes['gender']] ?? public_path('img/not_specified.png'),
}

tykus's avatar

@ene what is not working exactly? The image is not displaying because...???

  • What is the URL for the avatar image in this case?
  • Do you use m and f as the values in a gender column on the users table?
tykus's avatar
tykus
Best Answer
Level 104

@ene I see the issue... public_path was my mistake above; that is a file path 🤦‍♂️Use the asset helper instead:

    return [
        'm' => asset('img/male.png'),
        'f' => asset('img/female.png'),
        // ...
    ][$this->attributes['gender']] ?? asset('img/not_specified.png'),
Snapey's avatar

@ene

dont forget you can always look at the generated source in the browser to see what is being output by your template. It makes it much easier to see what is going wrong than just saying it does not work.

1 like

Please or to participate in this conversation.