My profile picture doesn't show in Laravel 6

480 Views Asked by At

I'm learning to create some forum, but when I update a photo profile, it can't show. When I try to inspect element it says:

"Failed to load resource: the server responded with a status of 404 (Not Found)"

This is my Controller:

public function update()
{
    $avatar = request()->file('avatar');
    $avatar_validate = 'image|mimes:jpeg,png,jpg,svg|max:2048';

    request()->validate([
        'username' => 'required|alpha_num|min:6|max:20|unique:users,username,' . auth()->id(),
        'name' => 'string|required',
        'avatar' => $avatar ? $avatar_validate : "",
    ]);

    $hash = auth()->user()->hash;

    $avatar_name = $avatar->storeAs('profile-picture', "{$hash}.{$avatar->extension()}");

    auth()->user()->update([
        'username' => request('username'),
        'name' => request('name'),
        'avatar' => $avatar_name,
    ]);

    return redirect()->route('users.show', auth()->user()->usernameOrHash());
}
1

There are 1 best solutions below

7
Leonel Becerra On

Try this:

$hash = auth()->user()->hash;

$filename = $hash . $avatar->getClientOriginalExtension();

$avatar_name = $avatar->storeAs('profile-picture', $filename);

Also maybe you have some "trash" files stored with a weird filename in your "profile-picture" directory