jeudi 21 mai 2020

Why my update image on Laravel 5.5 didn't work?

I have to do an edit data on my table master. User can edit the data and it work for the other, but when I change the image, it didn't change and didn't give an error when I update it. I use Laravel 5.5 and ere is the code I use :

This the controller for the update

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 = 'uluwatu_image/' . $fileName . '.' . $extension;
        }

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

This the code when user want to change 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" />
            <div class="form-group">
                <label for="exampleFormControlFile1">Change Image</label>
                <input type="file" class="form-control-file" id="objek_pict_edit" 
                name="objek_pict_edit">
            </div>
        </div>
    </div>
</div>

This is the controller store for user adding 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