app/Http/Controllers/DokterController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, $id)
    {
        $this->validate($request, [
            'name' => 'required|string',
            'address' => 'required',
            'tempat_lahir' => 'required',
            'tanggal_lahir' => 'required',
            'jenis_kelamin' => 'required',
            'spesialis' => 'required',
            'gambar' => 'nullable|image|mimes:jpg,png,jpeg|max:7000'
        ]);
        try {
            $dokter = Dokter::findOrFail($id);
            $photo = $dokter->gambar;
            if ($request->hasFile('gambar')) {
                !empty($photo) ? File::delete(public_path('uploads/dokter/' . $photo)) : null;
                $photo = $this->saveFile($request->name, $request->file('gambar'));
            }
            $dokter->update([
                'name' => $request->name,
                'address' => $request->address,
                'tempat_lahir' => $request->tempat_lahir,
                'tanggal_lahir' => $request->tanggal_lahir,
                'jenis_kelamin' => $request->jenis_kelamin,
                'spesialis' => $request->spesialis,
                'gambar' => $photo
            ]);
            return redirect('dokter')->with(['success' => 'Dokter : <strong>' . $dokter->name . '</strong> Diupdate']);
        } catch (Exception $e) {
            return redirect()->back()->with(['error' => $e->getMessage()]);
        }
    }
public function update ( Request $ request , $ id ) { $ this -> validate ( $ request , [ 'name' => 'required|string' , 'address' => 'required' , 'tempat_lahir' => 'required' , 'tanggal_lahir' => 'required' , 'jenis_kelamin' => 'required' , 'spesialis' => 'required' , 'gambar' => 'nullable|image|mimes:jpg,png,jpeg|max:7000' ] ) ; try { $ dokter = Dokter :: findOrFail ( $ id ) ; $ photo = $ dokter -> gambar ; if ( $ request -> hasFile ( 'gambar' ) ) { ! empty ( $ photo ) ? File :: delete ( public_path ( 'uploads/dokter/' . $ photo ) ) : null ; $ photo = $ this -> saveFile ( $ request -> name , $ request -> file ( 'gambar' ) ) ; } $ dokter -> update ( [ 'name' => $ request -> name , 'address' => $ request -> address , 'tempat_lahir' => $ request -> tempat_lahir , 'tanggal_lahir' => $ request -> tanggal_lahir , 'jenis_kelamin' => $ request -> jenis_kelamin , 'spesialis' => $ request -> spesialis , 'gambar' => $ photo ] ) ; return redirect ( 'dokter' ) -> with ( [ 'success' => 'Dokter : <strong>' . $ dokter -> name . '</strong> Diupdate' ] ) ; } catch ( Exception $ e ) { return redirect ( ) -> back ( ) -> with ( [ 'error' => $ e -> getMessage ( ) ] ) ; } }
Update the specified resource in storage.
Update the specified resource in storage .
//cek jika ada datd yang di kirim dari form //cek jika foto tidak kosong //maka foto yang lama akan terhapus //uploading file dengan menggunakan method saveFile() yg telah dibuat sebelumnya