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
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 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'),
}
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.