【问题标题】:The data fields like name, address and images are inserting into the database but the problem is that the image doesn't store to the folder名称,地址和图像等数据字段正在插入数据库中,但问题是图像没有存储到文件夹中
【发布时间】:2020-12-05 11:22:06
【问题描述】:

名称、地址和图像等数据字段正在插入到数据库中,但问题是图像没有存储到文件夹中。

公共函数存储(请求 $request) {

    $image = $request->file('file');
    $imageName = time().'.'.$image->extension();
    $image->move(public_path('images'),$imageName);

    $vendor = new Vendor();
           $vendor->profileimage = $imageName;

    $vendor->save();

    $vendor = $request->validate($this->validation());
    $value = Vendor::create($vendor);
 

    // if request has default vendor request
    if ($request->default_vendor) {
        $request->user()
            ->setting()
            ->update(['default_vendor_id' => $value->id]);
    }

    return $this->respondWithMessage('Vendor successfully saved.');
}

【问题讨论】:

  • 请将相关的视图代码以及您上传文件的位置发布。

标签: laravel nuxt.js


【解决方案1】:

您需要检查您是否收到上传的文件,是否有效

尝试 dd($request->hasFile('file') && $request->file('file')->isValid()) 来检查。

public function store(Request $request) {

    //Get the validated data from the request
    //Assuming that $this->validation() are the validation rules
    $validated = $request->validate($this->validation());

    //Check whether a valid file has been uploaded
    if($request->hasFile('file') && $request->file('file')->isValid()) {
        
        $image = $request->file('file');
        $imageName = time().'.'.$image->extension();
        $image->move(public_path('images'),$imageName);

        //Add the filename to the validated data array 
        $validated['profileImage'] = $imageName;
    }

    //Create a new Vendor record from the validated data
    //Exclude the file, as we have stored it already
    //And plus the file key contains file binary data
    //And we do not want to store actual file binary/blob data in db
    $vendor = Vendor::create(Arr::except($validated, ['file']));
 

    // if request has default vendor request
    if ($request->default_vendor) {
        $request->user()
            ->setting()
            ->update(['default_vendor_id' => $value->id]);
    }

    return $this->respondWithMessage('Vendor successfully saved.');
}

【讨论】:

    【解决方案2】:

    首先确保表单类型是多部分,然后尝试运行此命令:

    php artisan storage:link
    

    【讨论】:

      猜你喜欢
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-30
      • 2014-12-11
      • 1970-01-01
      • 2023-01-19
      • 2016-10-20
      • 1970-01-01
      相关资源
      最近更新 更多