Lolo Lolo - 1 year ago 126
PHP Question

Laravel 5.4+ : Mutator/Accessor for uploaded image

I'm trying to use mutator to store and retrieve users gravatar.

In my User model I have :

public function getGravatarAttribute($gravatar){
$image = Storage::disk('local')->get('public/avatars/'.$gravatar.'.jpg');
return new Response($image, 200);

$hash = md5(strtolower(trim($this->attributes['email'])));
return "$hash?s=256";

public function setGravatarAttribute($gravatar){
if(is_object($gravatar) && $gravatar->isValid()){
$image = Image::make($gravatar)->fit(256, 256);
Storage::disk('local')->put('public/avatars/'.$this->id . '.' . $gravatar->getClientOriginalExtension(), $image->response());
$this->attributes['gravatar'] = $this->id;

In the Blade file I do :

<img id="avatar" src="{{ $user->gravatar }}" alt="your image" width="256" height="256"/>

The mutator works nicely : the image is stored in the
directory. The problem is the accessor : if I use a
before the return I can see the datas but it's not displayed in the html page

Answer Source

You are trying to echo the Response facade like a string, however you just need to return the image path for the file.

You can use the url function on the Storage Facade to get the image path:

    $image = Storage::disk('local')->url('public/avatars/'.$gravatar.'.jpg');
    return $image;

Because you are using the value for the src:

src="{{ $user->gravatar }}"


From the docs:

Remember, if you are using the local driver, all files that should be publicly accessible should be placed in the storage/app/public directory. Furthermore, you should create a symbolic link at public/storage which points to the storage/app/public directory.

Once this is done your returned url should display your image.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download