mercredi 20 mai 2020

How to update Image on Laravel 5.5?

I trying to make an update image when user select the row on table. For the another update it's work, but when I'm try to update the image it nothing happen. I don't know where is my fault, here the code that i use.

Button for update the image

<div class="col-md-6 col-lg-6"> 
   <div class="col-md-6 col-lg-12">         
        <div class="card" style="width:100%">
      <img src=""  style="width:100%" alt="Card Image"/>
       " alt="Card image"> --}}
         
    <div class="form-group">
       <label for="exampleFormControlFile1">Ganti Gambar</label>
          <input type="file" class="form-control-file" id="objek_pict_edit" name="objek_pict_edit">
       </div>
       </div>
      </div>
        </div>

update Controller.php public function update(Request $request) {

    $this->validate($request, [
        'nama_objek_edit' => 'required',
        'objek_lat_edit' => 'required',
        'objek_lng_edit' => 'required',
        'objek_deskripsi_edit' => 'required',
        // 'objek_pict_edit' => 'required'

    ]);


    $updateObjek = TbLokasi::where('id_lokasi', $request->id)->first();
    $updateObjek->lokasi_nama = $request->nama_objek_edit;
    $updateObjek->lokasi_lat = $request->objek_lat_edit;
    $updateObjek->lokasi_lng = $request->objek_lng_edit;
    $updateObjek->lokasi_desc = $request->objek_deskripsi_edit;
    if ($request->hasFile('lokasi_pict')) {
        $filePic   = $request->file('lokasi_pict');
        $extension = $filePic->getClientOriginalExtension();
        $fileName  = date('m-d-Y_', time()) . $request->lokasi_nama;
        // $filePic->move('/uluwatu_image/', $fileName . '.' . $extension, file_get_contents($request->file('objek_pict')->getRealPath()));
        $filePic->move('uluwatu_image/', $fileName . '.' . $extension);
        $updateObjek->lokasi_pict = $request->objek_pict_edit;
    }
    $updateObjek->save();

    return redirect('/masterdata')->with('success', 'Data Berhasil Diubah');
}

And also, this is the code that I use for store the new data.

  public function store(Request $request)
    {
        if ($request->hasFile('objek_wto')) {
            $filePic   = $request->file('objek_wto');
            $extension = $filePic->getClientOriginalExtension();
            $fileName  = 'pura-uluwatu';
            // $filePic->move('/uluwatu_image/', $fileName . '.' . $extension, file_get_contents($request->file('objek_pict')->getRealPath()));
            $filePic->move('marker/', $fileName . '.' . $extension);
        }

        // ObjekTable::create($request->all());

        $new_objek = new TbObjek();
        $new_objek->objek_nama = $request->objek_nama;
        $new_objek->objek_wto = 'marker/' . $fileName . '.' . $extension;
        $new_objek->objek_desc = $request->objek_desc;
        $new_objek->save();

        return redirect('masterdataobjek')->with('success', 'Data Berhasil Ditambah');
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire