dimanche 25 novembre 2018

Laravel - Updating arrays values are not updating all

how can I update all of my images in an array in database? I've tried to update one out of three images but the other two images where removed why? only one left when I update more than one data? help me thanks.

I need to Update all of my images.

Here is it is in foreach loop because of the arrays.

Controller

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