简体   繁体   中英

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

Changing file names most simply can be done with ->store() method, something like this:

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(),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM