我使用的是Metronic 8管理面板模板,用户和配置文件在安装项目时已经开发好了。
我正在尝试upload
&Edit
用户配置文件图像。该文件正确保存在公共存储器中,也作为urlavatars/imagename.png
保存在数据库中
这是默认代码:
/**
* 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;
}
我试着把它改成这个,以便把文件保存到其他地方:
if( request()->hasfile($key) )
{
$file = request()->file($key);
$extension = $file->getClientOriginalExtension();
$filename = $key . auth()->user()->name . '.' . $extension;
$file->move('app/avatars/', $filename);
$key = $filename;
}
每次我在保存更改后刷新时,图像都会从UI中消失,但它实际上保存在公用文件夹中。图像的刀片:
<img src="{{ auth()->user()->avatar_url }}" alt="image"/>
此代码用于保存化身
// 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();
此代码用于错误处理(url属性(
/**
* 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');
}
尝试更改此代码
<img src="{{ auth()->user()->avatar_url }}" alt="image"/>
进入这个代码
<img src="{{ auth()->user->avatar_url }}" alt="image"/>