输入文件图像阵列更新-Laravel

时间:2018-11-27 00:08:39

标签: php mysql arrays laravel laravel-5

我想更新文件输入图像。更新一个图像后,其他图像将被删除。这是为什么?我想让他们保持新上传的内容。请帮助我,谢谢。

[这里是问题的图片] [1]

控制器

更新,公共功能,这是我放置代码逻辑的地方

public function update(Request $request, $id)
{
          $this->validate($request, [
        'inflightmagz_date' => 'required',
        'infightmagazine_pdf.*' => 'image|nullable|max:1999'
    ]);

    $inflightmags = [];

    if ($request->has('infightmagazine_pdf'))
    {   
        //Handle File Upload


        foreach ($request->file('infightmagazine_pdf') as $key => $file)
        {
            // Get FileName
            $filenameWithExt = $file->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
            //Get just extension
            $extension = $file->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            //Upload Image
            $path = $file->storeAs('public/infightmagazine_pdfs',$fileNameToStore);
            array_push($inflightmags, $fileNameToStore);
        }

        $fileNameToStore = serialize($inflightmags);
    }



    $inflightmagContent =  InflightMagazine::find($id);
    $inflightmagContent->inflightmagz_date = $request->input('inflightmagz_date');
       foreach ($inflightmags as $key => $value) {

        $implodedInflight = implode(' , ', $inflightmags);
        if($request->hasFile('infightmagazine_pdf')){
        $inflightmagContent->infightmagazine_pdf = $implodedInflight;
        }

    }
    $inflightmagContent->save();
    return redirect('/admin/airlineplus/inflightmagazines')->with('success', 'Content Updated');
}

查看,edit.blade.php

  {!! Form::open(['action'=>['Admin\FleetsController@update',$fleet->id], 'method' => 'POST','enctype'=>'multipart/form-data', 'name' => 'add_name', 'id' => 'add_name']) !!}

        <div class="table-responsive">
          <table class="table table-bordered" id="dynamic_field">
            <tr>
              <td>   {{Form::text('title', $fleet->title, ['class' => 'form-control', 'placeholder' => 'Enter a Title', 'id'=>"exampleFormControlFile1"])}}<br>

                {{Form::textarea('description', $fleet->description, ['class' => 'form-control', 'placeholder' => 'Enter a Description'])}} <br>
                <div class="card card-body col-md-8">

                @foreach(explode(' , ' ,$fleet->fleet_image) as $content)
                  <img src="{{ asset('storage/fleet_images/' . $content) }}" style="width:50px;height:50px;"><br/>

                  {{ Form::file('fleet_image[]',['id'=>'exampleFormControlFile1']) }}<br/>
                  @endforeach 
                </div>
             </td>

            </tr>
          </table>
          {{Form::hidden('_method', 'PUT')}}
          {{Form::submit('submit', ['class'=>'btn btn-primary', 'name'=>'submit'])}}
        </div>
    {!! Form::close() !!}

0 个答案:

没有答案