【问题标题】:How to retrieve uploaded file edit view blade using Laravel如何使用 Laravel 检索上传的文件编辑视图刀片
【发布时间】:2021-02-02 11:08:19
【问题描述】:

我在 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();
}

我使用的是模态形式:

edit.blade

<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

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

谢谢

【问题讨论】:

  • 您是要实际编辑文件,还是只编辑数据库行的值?
  • @Ballard - 我想显示文件名及其扩展名。我对编辑文本输入和其他没有问题。就像那里显示的文本输入一样。但它没有显示附件的任何内容

标签: laravel


【解决方案1】:

在值输入路径中使用文件

 <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">



      

【讨论】:

  • 我现在有 但是我收到了这个错误:语法错误,意外'')); ?>" value="
  • 您不需要获取旧文件。如果您在更新时没有在数据库和存储中更改它,它仍然会存在,或者如果是,它仍然会为空。您可以使用要从方法开头更新的数据准备数组并添加图像元素,前提是它存在。
  • 在你的控制器中 if($request->hasFile('employee_mid_year_attachment')){ update file}
  • 如果您需要检索旧文件,您可以在 img 标签中显示它 employee_mid_year_attachment')}}" >
  • 该文件不仅仅是图像。可以是pdf、excel
猜你喜欢
  • 2020-04-21
  • 1970-01-01
  • 2019-10-11
  • 1970-01-01
  • 1970-01-01
  • 2018-06-04
  • 1970-01-01
  • 2013-08-03
  • 1970-01-01
相关资源
最近更新 更多