簡體   English   中英

如何在laravel 5中更新文件而不丟失先前的文件

[英]How to update file in laravel 5 without losing the previous file

我有一個帶有多個輸入的表單,這是代碼:

@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');

}

簡單的邏輯,如果用戶要更新文件,則他們可能應該更新文件,並且文件應出現在請求中。 因此,只有文件出現在請求中並且有效,才更新文件。

if( $request->hasFile('file') && $request->file('file')->isValid()) 

對其他屬性(例如作者,標題...)執行相同的操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM