jeudi 1 septembre 2016

How to update file in laravel 5 without losing the previous file

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 ">
@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"></span>
    @endif
</div>

@if ($errors->any())
    <div class="form-group ">
@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"></span>
    @endif
</div>

@if ($errors->any())
    <div class="form-group ">
@else
    <div class="form-group">
@endif
    {!! Form::label('file', 'File Jurnal (PDF) :') !!}
    {!! Form::file('file') !!}
    @if ($errors->has('file'))
        <span class="help-block"></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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire