dimanche 25 novembre 2018

Laravel - File Input Array Update

I want to update my file input 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, [
        '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');
}

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