app/Http/Controllers/DokterController.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)
    {
        $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'
        ]);
        try {
            $photo = null;
            if ($request->hasFile('gambar')) {
                $photo = $this->saveFile($request->name, $request->file('gambar'));
            }
            $dokter = Dokter::create([
                '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> Ditambahkan']);
        } catch (Exception $e) {
            return redirect()->back()->with(['error' => $e->getMessage()]);
        }
    }
public function store ( Request $ request ) { $ 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' ] ) ; try { $ photo = null ; if ( $ request -> hasFile ( 'gambar' ) ) { $ photo = $ this -> saveFile ( $ request -> name , $ request -> file ( 'gambar' ) ) ; } $ dokter = Dokter :: create ( [ '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> Ditambahkan' ] ) ; } catch ( Exception $ e ) { return redirect ( ) -> back ( ) -> with ( [ 'error' => $ e -> getMessage ( ) ] ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .
//default photo = null //jika terdapat file gambar yang dikirim  //maka menjalankan method save file