我的个人资料照片未在Laravel 6中显示

时间:2019-11-22 16:06:38

标签: php laravel laravel-6 profile-picture

我正在学习创建一些论坛,但是当我更新照片个人资料时,它无法显示。当我尝试检查元素时,它说:

  

“无法加载资源:服务器响应状态为404(未找到)”

这是我的控制器:

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 个答案:

答案 0 :(得分:1)

尝试一下:

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

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

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

也许您的“个人资料图片”目录中存储了一些带有奇怪文件名的“垃圾”文件