如何使用经过验证的方法处理图像 - 表单请求 (Laravel 9)

How to deal with Image using validated method - Form Request (Laravel 9)

我的表单有 4 个字段 name, email, website and image


当只有 name, email, website 字段被传递时,下面的代码 将数据 完美地保存到我的数据库中。

public function store(StoreCompanyRequest $request)
    {
         $validated = $request->validated(); //It will return only validated data
         Company::create($validated); 
     
         return response()->json([
            'success' => true,
            'message' => 'Company Created Successfully',
        ]);
    }

但是如果用户传递 image file。上面的代码保存图片的临时路径。

这是输出:

输出: $validated

array:4 [  "name" => "infotech"  "email" => "infotech@gmail.iop"  "logo" => Illuminate\Http\UploadedFile {#310  -test: false  -originalName: "greenscreenman.jpg"  -mimeType: "image/jpeg"  -error: 0  #hashName: null  path: "C:\Users\dummy\AppData\Local\Temp"  filename: "php30CD.tmp"  basename: "php30CD.tmp"  pathname: "C:\Users\dummy\AppData\Local\Temp\php30CD.tmp"  extension: "tmp"  realPath: "C:\Users\dummy\AppData\Local\Temp\php30CD.tmp"  aTime: 2022-03-22 12:52:30  mTime:  2022-03-22 12:52:29  cTime: 2022-03-22 12:52:29  inode: 258750  size: 70458  perms: 0100666  owner: 0  group: 0  type: "file"  writable: true  readable: true  executable: false  file: true  dir: false  link: false  linkTarget: "C:\Users\dummy\AppData\Local\Temp\php30CD.tmp"  }  "website" => "www.infotech.com" ]

我写了另一个代码来解决这个问题,

public function store(StoreCompanyRequest $request)
    {
         $validated = $request->validated(); //It will return only validated data
        //  Company::create($validated); 

         $company = new Company;
         $company->name = $validated['name'];
         $company->email = $validated['email'];
         $company->website = $validated['website'];

         $logoName = time().'.'.$request->file('logo')->extension(); 
         $logoPath = $request->file('logo')->storeAs('public/files', $logoName);

         $company->logo = $logoName;
         $company->save();

         return response()->json([
            'success' => true,
            'message' => 'Company Created Successfully',
        ]);
    }

但是有没有办法用第一个代码块保存图像的正确位置???

is there any way to save the right location of the image with the very first code block

不,因为第一个代码块只是在保存之前验证您的输入。文件上传需要更多的工作。 The docs 是一个很好的起点。

这是我在评论中描述的内容。

如果您需要对文件名进行一些控制:

public function store(StoreCompanyRequest $request)
{
    $validated = $request->validated();

    $path = $request->file('logo')->storeAs(
        'public/files', 
        time() . '.' . $request->file('logo')->extension()
    );

    $validated['logo'] = basename($path);

    Company::create($validated); 

     return response()->json([
        'success' => true,
        'message' => 'Company Created Successfully',
    ]);
}

如果您不需要控制文件名,您可以让 Laravel 为您做更多的工作:

public function store(StoreCompanyRequest $request)
{
    $validated = $request->validated();

    $path = $request->file('logo')->store('public/files');
    $validated['logo'] = basename($path);

    Company::create($validated); 

     return response()->json([
        'success' => true,
        'message' => 'Company Created Successfully',
    ]);
}