在Laravel 6中保存在MySQL中的验证和上传的图像名称不正确

问题描述 投票:0回答:1

文本和照片的验证在StorePost FormRequest中进行。

public function rules()
    {
        return [
            'name' => 'required',
            'exerpt => 'required',
            'photo' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ];
    }

然后是控制器部分:

public function store( StorePost $request )
    {
        $imageName = time().'.'.$request->photo->extension();  
        $request->photo->move(public_path('post-images'), $imageName);

        // may modify image name here but it's not elegant
        //$data = $request->all();
        //$data['photo'] = $imageName;

        Post::create( $request->all() );
    }

图像在MySQL中另存为/private/var/folders/zr/y1drl_rs0sl75rxvgkx8ntzm0000gn/T/phpUJKeEG。如何在请求到达控制器之前设置其名称?

我不想在这里这样(注释行)。

php laravel request image-uploading
1个回答
0
投票

您可以使用

$imageName = time().'_'.$request->photo->extension();  
$request->photo->storeAs('public/post-images',$imageName);

$post = new Post;
//...
$post->photo = $imageName;
//...
$post->save();
© www.soinside.com 2019 - 2024. All rights reserved.