app/Http/Controllers/PetugasController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        DB::beginTransaction();
        $this->validate($request,[
            "username" => "required|unique:users,username",
            "nama" => "required",
            "password" => "required",
            "id_level" => "required",
            "nip" => "required",
            "alamat" => "required",
            "photo" => "required"
        ]);
        $fileName = Str::random(32).".".$request->photo->getClientOriginalExtension();
        $request->photo->move('images',$fileName);
        $user = new User([
            "username" => $request->username,
            "password" => $request->password,
            "id_level" => $request->id_level,
        ]);
        if($user->save()){
            $petuga = new Petugas([
                "nama" => $request->nama,
                "alamat" => $request->alamat,
                "id_user" => $user->id,
                "photo" => $fileName,
                "nip" => $request->nip
            ]);
            if($petuga->save()){
                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 store ( Request $ request ) { DB :: beginTransaction ( ) ; $ this -> validate ( $ request , [ " username " => " required|unique:users,username " , " nama " => " required " , " password " => " required " , " id_level " => " required " , " nip " => " required " , " alamat " => " required " , " photo " => " required " ] ) ; $ fileName = Str :: random ( 32 ) . " . " . $ request -> photo -> getClientOriginalExtension ( ) ; $ request -> photo -> move ( 'images' , $ fileName ) ; $ user = new User ( [ " username " => $ request -> username , " password " => $ request -> password , " id_level " => $ request -> id_level , ] ) ; if ( $ user -> save ( ) ) { $ petuga = new Petugas ( [ " nama " => $ request -> nama , " alamat " => $ request -> alamat , " id_user " => $ user -> id , " photo " => $ fileName , " nip " => $ request -> nip ] ) ; if ( $ petuga -> save ( ) ) { DB :: commit ( ) ; return redirect ( ) -> route ( 'petugas.index' ) -> with ( 'success' , 'Data berhasil ditambahkan' ) ; } } DB :: rollback ( ) ; return redirect ( ) -> route ( 'petugas.index' ) -> with ( 'error' , 'Data gagal ditambahkan' ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .