Reputation: 1421
I have a form with multiple input in it, here is the code :
@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>
And here is the controller :
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');
}
These data are editable including the file, what I wanted to know is that how is the update function would look like ? For example, the user wanted to update the title or the author data but not the file, how to do it without losing the previous file ? I am new to Laravel 5 and I'm stuck here, please help, thank you.
Here is the update function I created, it works fine if I update all the data, won't work if I update anything else beside the file, it always requires me to upload a new one
public function update(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');
}
Upvotes: 0
Views: 2929
Reputation: 15089
Simple logic, if users want to update file, they probably should update the file and file should present on the request. So only update file if file is presented on the request and valid.
if( $request->hasFile('file') && $request->file('file')->isValid())
Do the same thing for other properties like author, title...
Upvotes: 1