app/Http/Controllers/PetugasController.php
/**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Petugas  $petuga
     * @return \Illuminate\Http\Response
     */
Update the specified resource in storage.
Update the specified resource in storage .
public function update(Request $request, Petugas $petuga)
    {
        DB::beginTransaction();
        $user = User::findOrFail($petuga->id_user);
        $this->validate($request,[
            "username" => "required",
            "nama" => "required",
            "id_level" => "required",
            "alamat" => "required",
        ]);
        if($request->hasFile('photo')){
            if(File::exists('images/'.$petuga->photo)){
                unlink('images/'.$petuga->photo);
            }
            $fileName = Str::random(32).".".$request->photo->getClientOriginalExtension();
            $request->photo->move('images',$fileName);
        } else {
            $fileName = $petuga->photo;
        }
        $user->username = $request->username;
        if($request->password != ""){
            $user->password = $request->password;
        }
        $user->id_level = $request->id_level;
        if($user->update()){
            $petuga->nama = $request->nama;
            $petuga->alamat = $request->alamat;
            $petuga->nip = $request->nip;
            $petuga->photo = $fileName;
            if($petuga->update()){
                DB::commit();
                return redirect()->route('petugas.index')->with('success','Data berhasil ditambahkan');
            }
        }
        DB::rollback();
        return redirect()->route('petugas.index')->with('error','Data gagal ditambahkan');
    }
public function update ( Request $ request , Petugas $ petuga ) { DB :: beginTransaction ( ) ; $ user = User :: findOrFail ( $ petuga -> id_user ) ; $ this -> validate ( $ request , [ " username " => " required " , " nama " => " required " , " id_level " => " required " , " alamat " => " required " , ] ) ; if ( $ request -> hasFile ( 'photo' ) ) { if ( File :: exists ( 'images/' . $ petuga -> photo ) ) { unlink ( 'images/' . $ petuga -> photo ) ; } $ fileName = Str :: random ( 32 ) . " . " . $ request -> photo -> getClientOriginalExtension ( ) ; $ request -> photo -> move ( 'images' , $ fileName ) ; } else { $ fileName = $ petuga -> photo ; } $ user -> username = $ request -> username ; if ( $ request -> password != " " ) { $ user -> password = $ request -> password ; } $ user -> id_level = $ request -> id_level ; if ( $ user -> update ( ) ) { $ petuga -> nama = $ request -> nama ; $ petuga -> alamat = $ request -> alamat ; $ petuga -> nip = $ request -> nip ; $ petuga -> photo = $ fileName ; if ( $ petuga -> update ( ) ) { DB :: commit ( ) ; return redirect ( ) -> route ( 'petugas.index' ) -> with ( 'success' , 'Data berhasil ditambahkan' ) ; } } DB :: rollback ( ) ; return redirect ( ) -> route ( 'petugas.index' ) -> with ( 'error' , 'Data gagal ditambahkan' ) ; }
Update the specified resource in storage.
Update the specified resource in storage .