【发布时间】:2017-01-09 12:50:19
【问题描述】:
我有一个包含多个输入的表单,代码如下:
@if (isset($jurnal))
{!! Form::hidden('id', $jurnal->id) !!}
@endif
@if ($errors->any())
<div class="form-group {{ $errors->has('title') ? 'has-error' : 'has-success' }}">
@else
<div class="form-group">
@endif
{!! Form::label('title', 'Title :', ['class' => 'control-label']) !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
@if ($errors->has('title'))
<span class="help-block">{{ $errors->first('title') }}</span>
@endif
</div>
@if ($errors->any())
<div class="form-group {{ $errors->has('author') ? 'has-error' : 'has-success' }}">
@else
<div class="form-group">
@endif
{!! Form::label('author', 'Author :', ['class' => 'control-label']) !!}
{!! Form::text('author', null, ['class' => 'form-control']) !!}
@if ($errors->has('author'))
<span class="help-block">{{ $errors->first('author') }}</span>
@endif
</div>
@if ($errors->any())
<div class="form-group {{ $errors->has('file') ? 'has-error' : 'has-success' }}">
@else
<div class="form-group">
@endif
{!! Form::label('file', 'File Jurnal (PDF) :') !!}
{!! Form::file('file') !!}
@if ($errors->has('file'))
<span class="help-block">{{ $errors->first('file') }}</span>
@endif
</div>
<div class="form-group">
{!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}
</div>
这里是控制器:
private function uploadPDF(JurnalRequest $request)
{
$file = $request->file('file');
$ext = $file->getClientOriginalExtension();
if ($request->file('file')->isValid()) {
$pdf_name = date('YmdHis'). ".$ext";
$upload_path = 'pdfupload';
$request->file('file')->move($upload_path, $pdf_name);
// Filename untuk database --> 20160220214738.pdf
return $pdf_name;
}
return false;
}
public function store(JurnalRequest $request)
{
$input = $request->all();
//Input PDF
if ($request->hasFile('file')) {
$input['file'] = $this->uploadPDF($request);
}
//Insert data jurnal
$jurnal = Jurnal::create($input);
return redirect('jurnal');
}
这些数据包括文件都是可编辑的,我想知道的是更新功能是什么样的?例如,用户想更新标题或作者数据而不是文件,如何在不丢失以前的文件的情况下做到这一点?我是 Laravel 5 的新手,我被困在这里,请帮助,谢谢。
这是我创建的更新功能,如果我更新所有数据,它可以正常工作,如果我更新文件之外的任何其他内容,它将无法正常工作,它总是需要我上传一个新的
公共功能更新(Jurnal $jurnal, JurnalRequest $request) { $input = $request->all();
// Check is there a new file in the form?
if ($request->hasFile('file')) {
// Delet old file
$this->hapusPDF($jurnal);
// Upload new file
$input['file'] = $this->uploadPDF($request);
}
// Update jurnal di tabel jurnal
$jurnal->update($input);
return redirect('jurnal');
}
【问题讨论】: