【问题标题】:Laravel: Updating multiple files in arrayLaravel:更新数组中的多个文件
【发布时间】:2021-11-04 23:43:15
【问题描述】:

我能够将数组中的多个文件存储并保存到数据库中,以及更新表单数据。但是,现在的问题是,当我更新表单而不更新file 时,它会返回此错误。

count():参数必须是数组或者实现了Countable的对象

有谁知道我应该为我的刀片文件中的update 函数或value 做些什么,这样它就不会删除file 元素?

---------------------------------------------- 已更新 -------------------------------------------- -

public function update(Request $request, $id)
{        
    $data = $request->all();
    $data = $this->saveEmptyData($data);

    $c = Document::find($id);
    $fileData = [];

    if(count($request->file)>0)
    {
        foreach($request->file as $file)
        {    
            $path = public_path('storage/documents');
            $fileName = time().'_'.$file->getClientOriginalName();
            $file->move($path, $fileName);

            if ($c->file)
            {
                File::delete($path.'/'.json_decode($c->file)[0]);
            }
            $fileData[] = $fileName;
        }
        $doc = json_encode($fileData);
        $c->file = $doc;
        
        $c->name = $request->name;
        $c->price = $request->price;
        $c->publish = $request->publish;
    } 
    //else {
    //    redirect()->back()->with('errors', 'File is required!');
    //}
    $c->save();

    return redirect()->route('product.index')->with('success', 'Document updated successfully');
}

刀片

<form method="POST" action="{{ route('document.update', $doc) }}" enctype="multipart/form-data">
    <fieldset>
        ....
        <div class="form-group">
            <label>Document</label>
            <div class="input-group-append">
                <label for="upload_file">Upload</label>
                <input id="upload_file" type="file" name="file[]" value="{{ isset($doc) ? $doc['file'] : '' }}" multiple>
            </div>
        </div>
    </fieldset>
</form>

【问题讨论】:

    标签: arrays laravel file upload


    【解决方案1】:
     if(count($request->file)>0)
     {
       foreach($request->file as $file)
       { 
          $path = public_path('documents/');
          $fileName = time().'_'.$file->getClientOriginalName();
          $file->move($path, $fileName);
    
          if ($c->file)
          {
             File::delete($path.'/'.json_decode($c->file)[0]);
          }
    
          $fileData[] = $fileName;
       }
       $doc= json_encode($fileData); //replace $doc['file'] with $doc
       $c->file=$doc;
     }
    
    $c->save();
    //not required to declare $fileData ;
    

    【讨论】:

    • 感谢您的回复,file 终于更新了,但是当我更新表单而不更新file 时它返回count(): Parameter must be an array or an object that implements Countable
    【解决方案2】:

    enctype="multipart/form-data" 在表单标签中

    if(count($request->file)>0)
    {
        foreach($request->file as $file)
        { 
            $path = public_path('documents/');
            $fileName = time().'_'.$file->getClientOriginalName();
            $file->move($path, $fileName);
    
            if ($c->file)
            {
                File::delete($path.'/'.json_decode($c->file)[0]);
            }
    
            $fileData[] = $fileName;
        }
        $doc['file'] = json_encode($fileData);
    }
    

    【讨论】:

    • 请解释您的解决方案。没有解释且只有代码的答案会被标记为省力。
    • 它为我的数据库中的file 列返回[{},{}]
    猜你喜欢
    • 2019-04-27
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多