xuuto's avatar
Level 2

laravel 8 uploaded image not displaying returning 404 error page

i have a mutator which returns current user uploaded image avatar

 public function getAvatarAttribute($value)
    {
        return $value ? asset('storage/' . $value) : asset('/img/defualt-avatar.png');
    
    }

and in my controller ProfileController iam updating user details and avatar image if selected.

public function update(User $user)
    {

        $attributes = request()->validate([
            'username' => ['string', 'required', 'max:255', 'alpha_dash', Rule::unique('users')->ignore($user)],
            'name' => ['string', 'required', 'max:255'],
            'bio' => ['string'],
            'email' => ['string', 'required', 'email', 'max:255', Rule::unique('users')->ignore($user)],
            'avatar' => ['file'],
            'banner' => ['file'],
            'password' => ['string', 'required', 'min:8', 'max:255', 'confirmed'],
        ]);

        if (request('avatar')) {
            $fileName = 'profile-' . time() . '.' . $attributes['avatar']->getClientOriginalExtension();
            $attributes['avatar'] = request('avatar')->storeAS('avatars', $fileName);
        }

        $user->update($attributes);

        return redirect($user->path());
    }

the file uploads to storage app avatars folder and i made php artisan storage:link but is not public storage avatars folder

when i visit this url for the uploaded file

http://localhost:8000/storage/avatars/profile-1605503881.png

it returns 404 page

0 likes
4 replies
ideepesh's avatar

@xuuto Because the file is not getting stored. You have a syntax mistake use 'storeAs' not 'storeAS'

$request->file('avatar')->storeAs('avatars', $fileName);
xuuto's avatar
Level 2

idid, i even set it store('avatars') inorder to gernerate filename, but the problem is it is uploading in storege/app/avatars but there are not any files in public/storage/avatars folder at all. eventhough i linked using php artisan storage:link to link directories

ideepesh's avatar
ideepesh
Best Answer
Level 10

@xuuto To get the file available in public/storage/avatars your file should upload at storage/app/public/avatars. can you provide the updated code..? to change the uploads to the storage/app/public you should update the env file. Add this

FILESYSTEM_DRIVER="public"
2 likes
xuuto's avatar
Level 2

yeah it worked, thanks

1 like

Please or to participate in this conversation.