laravel-更新输入文件中上传的文件

i1icjdpr  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(325)

我想更新我上传的图片,但是一旦我更新了一个图片,其他的图片就会被删除为什么?我想离开他们,因为他们是什么时候被上传。请帮帮我谢谢。
这是问题的图像
控制器
更新,公共函数,这里是我放代码逻辑的地方

public function update(Request $request, $id)
{
    $this->validate($request, [
        'title' => 'required',
        'description' => 'required',
        'fleet_image.*' => 'image|nullable|max:1999'
    ]);
    $fleet = [];
    if ($request->has('fleet_image'))
    {   
        //Handle File Upload

        foreach ($request->file('fleet_image') 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/fleet_images',$fileNameToStore);
            array_push($fleet, $fileNameToStore);
        }

        $fileNameToStore = serialize($fleet);
    }
    else
    {
        $fileNameToStore='noimage.jpg';
    }

    if (count($fleet)) {
        foreach ($fleet as $key => $value) {
        $fleetContent = Fleet::find($id);
        $fleetContent->title = $request->title[$key];
        $fleetContent->description = $request->description[$key];
        $implodedFleet = implode(' , ', $fleet);
        if($request->hasFile('fleet_image')){
       $fleetContent->fleet_image = $implodedFleet;
        }
       $fleetContent->save();
       return redirect('/admin/airlineplus/fleets')->with('success', 'Content Updated');
    }

}
return redirect('/admin/airlineplus/promotions')->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() !!}
oxosxuxt

oxosxuxt1#

在你的控制器上

$fleet = array();
$fleetContent = Fleet::find($id);
if ($request->has('fleet_image')) {
    for($i=0;$i<3;$i++){
        if(isset($request->file('fleet_image')[$i])){
            $filenameWithExt = $request->file('fleet_image')[$i]->getClientOriginalName();
            //Get just filename
            $filename = pathinfo( $filenameWithExt, PATHINFO_FILENAME);
            //Get just extension
            $extension = $request->file('fleet_image')[$i]->getClientOriginalExtension();
            //Filename to Store
            $fileNameToStore = $filename.'_'.time().'.'.$extension;
            //Upload Image
            $path = $request->file('fleet_image')[$i]->move('public/fleet_images',$fileNameToStore);
            array_push($fleet, $fileNameToStore);
        }else{
            $fleetContentExplode = explode(',',$fleetContent->fleet_image);
            array_push($fleet,$fleetContentExplode[$i]);
        }
    }
}

在您的视图文件上

{!! 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() !!}

注意:我刚刚从视图输入文本中删除了数组

相关问题