我有一个包含多个输入的表单,这里是代码:
@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-&gt; 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');
}
答案 0 :(得分:1)
简单的逻辑,如果用户想要更新文件,他们可能应该更新文件和文件应该出现在请求上。因此,只有在请求中显示文件且有效时才更新文件。
if( $request->hasFile('file') && $request->file('file')->isValid())
对 author,title ...
等其他属性执行相同的操作