dimanche 25 novembre 2018

Laravel - Update Uploaded Files in Input File

I want to update my uploaded images, but as soon as I update 1 image the other images are removed why is that? I want to left them as what they are when they had been upload. help me please thanks.

Here is an image of the problem

Controller

Update, public function, here is where I put the logic of the code

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


}

View, 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>   <br>

                 <br>
                <div class="card card-body col-md-8">

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

                  <br/>
                  @endforeach 
                </div>
             </td>

            </tr>
          </table>
          
          
        </div>
    {!! Form::close() !!}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire