0

I am using Metronic 8 Admin Dashboard template, user and profile was already developed when installed the project.

I am trying to upload & Edit user profile image. The file is saved correctly in the public storage and also in the database as a url avatars/imagename.png

This is the default code:

/**
     * Function for upload avatar image
     *
     * @param  string  $folder
     * @param  string  $key
     * @param  string  $validation
     *
     * @return false|string|null
     */
    public function upload($folder = 'avatars', $key = 'avatar', $validation = 'image|mimes:jpeg,png,jpg,gif,svg|sometimes')
    {
        request()->validate([$key => $validation]);

        $file = null;
        if (request()->hasFile($key)) {
            $file = Storage::disk('public')->putFile($folder, request()->file($key), 'public');
        }

        return $file;
    }

I tried changing it to this, in order to save the file somewhere else:

if( request()->hasfile($key) )
        {
            $file = request()->file($key);
            $extension = $file->getClientOriginalExtension();
            $filename = $key . auth()->user()->name . '.' . $extension;
            $file->move('app/avatars/', $filename);
            $key = $filename;
        }

Every time I refresh after saving changes, the image is gone from the UI but it's actually saved in the public folder. The blade for the image:

<img src="{{ auth()->user()->avatar_url }}" alt="image"/>

This code is for saving the avatar

// include to save avatar
        if ($avatar = $this->upload()) {
            $info->avatar = $avatar;
        }

        if ($request->boolean('avatar_remove')) {
            Storage::delete($info->avatar);
            $info->avatar = null;
        }

        $info->save();

This code is for error handling (url attribute)

/**
     * Prepare proper error handling for url attribute
     *
     * @return string
     */
    public function getAvatarUrlAttribute()
    {
        if ($this->info) {
            return asset($this->info->avatar_url);
        }

        return asset(theme()->getMediaUrlPath().'avatars/blank.png');
    }
Abdulrahman Mushref
  • 1,012
  • 2
  • 18
  • 40

1 Answers1

0

Try to change this code

<img src="{{ auth()->user()->avatar_url }}" alt="image"/>

Into this code

<img src="{{ auth()->user->avatar_url }}" alt="image"/>
Mansjoer
  • 174
  • 6