[英]Laravel Nova Image Storage Name Issue
So, I am trying to store an image that is uploaded through a blog post to a specific folder with its original file name.因此,我正在尝试将通过博客文章上传的图像存储到具有原始文件名的特定文件夹中。
The image is saved to the correct file path already but it's saving it as a random string name.图像已保存到正确的文件路径,但将其保存为随机字符串名称。 My code is below:我的代码如下:
public function fields(Request $request)
{
return [
ID::make('id')->sortable(),
Text::make('URL ID', 'id')->hideFromIndex(),
Text::make('Title', 'title'),
select::make('Market Type', 'market_id')->options([
'church' => 'Church',
'school' => 'School',
'business' => 'Business',
'municipal' => 'Municipal'
]),
Trix::make('Body', 'text'),
Image::make('Image', 'main_image')
->disk('blog')
->storeOriginalName('main_image')
->maxWidth(200)
->prunable(),
];
}
->storeOriginalName() is not about name of the saved file, but name of file that browser gets when clicking 'download' in the image field in detail page ->storeOriginalName() 不是保存文件的名称,而是浏览器在详细页面的图像字段中单击“下载”时获取的文件名称
Changing file names most simply can be done with ->store() method, something like this:最简单的更改文件名可以使用 ->store() 方法完成,如下所示:
Image::make('Image', 'main_image')
->store(function (Request $request, $model) {
$filename = $request->main_image->getClientOriginalName();
$request->main_image->storeAs('/', $filename, 'blog');
return [
'main_image' => '/' . $filename,
'main_image_name' => $request->main_image->getClientOriginalName()
];
})
->maxWidth(200)
->storeOriginalName('main_image_name')
->prunable(),
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.