首页 > 解决方案 > 如何使用 Laravel 检索上传的文件编辑视图刀片

问题描述

我在 Laravel-5.8 中有一个项目,用于上传 excel 和图像等文件。

我已经成功插入了。我将文件名保存在表中,并将文件本身保存在 a 中。我遇到的问题是如何在编辑视图刀片中检索文件。

控制器

public function update_employee_mid_year_comment(UpdateSelfReviewRequest $request, $id) {
    $goal = Goal::find($id);  

    $goal->employee_mid_year_comment = $request->employee_mid_year_comment;

    if ($request->employee_mid_year_attachment != "") {
        $employee_mid_year_attachment = $request->file('employee_mid_year_attachment');
        $new_name = rand() . '.' . $employee_mid_year_attachment->getClientOriginalExtension();
        $employee_mid_year_attachment->move(public_path('storage/documents/mid_year'), $new_name);
        $goal->employee_mid_year_attachment = $new_name;
    }
    $goal->save();

    DB::commit();

    Session::flash('success', 'Comment is Successfully Updated');
    return redirect()->back();
}

我正在使用模态形式:

编辑刀片

<div class="modal fade" id="edit{{ $goal->id }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
    <form action="{{route('mid_year_setups.update_employee_mid_year_comment',['id'=>$goal->id])}}" method="post" enctype="multipart/form-data" id="edit_comment-form">
    {{ csrf_field() }}
    <div class="modal-header">
        Update Self-Review Comment
    </div>
    <div class="col-md-12">
        <div class="form-group">
        <label class="control-label">Comment:<span style="color:red;">*</span></label>
        <textarea rows="2" name="employee_mid_year_comment" class="form-control" placeholder="Enter Comment here" value="{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}})}}" required data-validation-required-message="This field is required">{{old('employee_mid_year_comment',$goal->employee_mid_year_comment)}}</textarea>
        </div>
    </div>
    <div class="col-md-12">
        <div class="form-group">
        <label class="control-label"> Attachment:</label>
        <div class="custom-file">
            <div class="custom-file">
            <input value="{{old('employee_mid_year_attachment',$goal->employee_mid_year_attachment)}}" type="file" name="employee_mid_year_attachment" class="custom-file-input" id="customFile">
            <label class="custom-file-label" for="exampleInputFile">Choose file</label>
            </div>
        </div>
        </div>
    </div>
    <div class="modal-footer">
        <button type="submit" id="edit_comment_btn-submit" class="btn btn-success btn-ok">Save</button>
    </div>
    </form>
</div>
</div>
</div>

文件名是employee_mid_year_attachment,文件路径是:storage/documents/mid_year

当我渲染编辑视图刀片时,它没有检索到附加的文档。我如何实现这一目标?

谢谢

标签: laravel

解决方案


在文件的值输入路径中使用

 <input 
 value="{{asset('storage/documents/mid_year/'.$goal>employee_mid_year_attachment')}}" 
  type="file" name="employee_mid_year_attachment" class="custom-file-input" 
  id="customFile">



      

推荐阅读