From cbcc053ede8bd84f4c7b4bea92f8e51781f91596 Mon Sep 17 00:00:00 2001 From: Gunawan19621 Date: Tue, 29 Aug 2023 23:31:58 +0700 Subject: [PATCH] keempat --- app/Http/Controllers/ProfileController.php | 74 ++++++++++++++------- app/Http/Requests/UpdatePenggunaRequest.php | 33 ++++++++- resources/views/layouts/main.blade.php | 43 ++++++++++++ resources/views/profil/profil.blade.php | 4 +- routes/web.php | 10 +-- 5 files changed, 129 insertions(+), 35 deletions(-) diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index dbfaea8..0e21e6e 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -29,39 +29,67 @@ class ProfileController extends Controller } //Proses update Profile - public function update(UpdatePenggunaRequest $request, $id) + public function updateprofile(Request $request, $id) { + $request->validate([ + 'name' => 'required|string|max:255|min:3|regex:/^[a-zA-Z\s]+$/', + 'email' => 'required|email|max:255|min:3', + 'phone' => 'required|numeric|digits_between:10,13', + 'foto' => 'nullable|image|mimes:jpg,jpeg,png|max:2048', + 'tgl_lahir' => 'date', + 'jenis_kelamin' => 'required|in:L,P', + 'agama' => 'required|in:Islam,Kristen,Katolik,Hindu,Budha,Konghucu', + 'alamat' => 'required|string|max:255|min:3', + ]); + try { $pengguna = User::findOrFail($id); - $pengguna->update($request->validated()); - return redirect()->route('profile.edit')->with('success', 'Profil pengguna berhasil perbaharui'); + + // Handle foto upload if provided + if ($request->hasFile('foto')) { + // Upload and update foto + $fotoPath = $request->file('foto')->store('profile_photos', 'public'); + $pengguna->foto = $fotoPath; + } + + // Update other fields + $pengguna->name = $request->name; + $pengguna->email = $request->email; + $pengguna->phone = $request->phone; + $pengguna->tgl_lahir = $request->tgl_lahir; + $pengguna->jenis_kelamin = $request->jenis_kelamin; + $pengguna->agama = $request->agama; + $pengguna->alamat = $request->alamat; + + $pengguna->save(); + + return redirect()->back()->with('success', 'Profil pengguna berhasil diperbarui'); } catch (\Throwable $th) { - dd($th); - return redirect()->route('pengguna.index')->with('error', 'Data pengguna gagal diubah'); + // Uncomment this line to see the error details + // dd($th); + return redirect()->back()->with('error', 'Data pengguna gagal diubah'); } } - // public function updateprofile(Request $request) // { // // dd('oke'); - // $messages = [ - // 'tgl_lahir.date_format' => 'Format tanggal lahir harus sesuai dengan d/m/Y.', - // ]; - + // $request->validate([ + // 'name' => 'required|string|max:255|min:3|regex:/^[a-zA-Z\s]+$/', + // 'email' => 'required|email|max:255|min:3', + // 'phone' => 'required|numeric|digits_between:10,13', + // 'foto' => 'nullable|image|mimes:jpg,jpeg,png|max:2048', + // 'tgl_lahir' => 'date', + // 'jenis_kelamin' => 'required|in:L,P', + // 'agama' => 'required|in:Islam,Kristen,Katolik,Hindu,Budha,Konghucu', + // 'alamat' => 'required|string|max:255|min:3', + // ]); + // // dd($request); // try { - // $request->validate([ - // 'name' => 'required', - // 'email' => 'required', - // 'phone' => 'required', - // 'tgl_lahir' => 'date', - // 'jenis_kelamin' => 'required', - // 'agama' => 'required', - // 'alamat' => 'required', - // ], $messages); - // // dd($request->all()); - // return back()->with('success', 'Profil berhasil di update.'); + // $pengguna = User::findOrFail($id); + // $pengguna->update($request->validated()); + // return redirect()->back()->with('success', 'Profil pengguna berhasil diperbarui'); // } catch (\Throwable $th) { - // // dd($th); - // return back()->with('eror', 'Profil gagal di update.'); + // dd($th); + // return redirect()->back()->with('error', 'Data pengguna gagal diubah'); // } // } diff --git a/app/Http/Requests/UpdatePenggunaRequest.php b/app/Http/Requests/UpdatePenggunaRequest.php index 452d807..792c641 100644 --- a/app/Http/Requests/UpdatePenggunaRequest.php +++ b/app/Http/Requests/UpdatePenggunaRequest.php @@ -11,18 +11,45 @@ class UpdatePenggunaRequest extends FormRequest */ public function authorize(): bool { - return false; + return true; } /** * Get the validation rules that apply to the request. * - * @return array + * @return array */ public function rules(): array { return [ - // + 'name' => 'required|string|max:255|min:3|regex:/^[a-zA-Z\s]+$/', + 'email' => 'required|email|max:255|min:3', + 'phone' => 'required|numeric|digits_between:10,13', + 'foto' => 'nullable|image|mimes:jpg,jpeg,png|max:2048', + 'tgl_lahir' => 'date', + 'jenis_kelamin' => 'required|in:L,P', + 'agama' => 'required|in:Islam,Kristen,Katolik,Hindu,Budha,Konghucu', + 'alamat' => 'required|string|max:255|min:3', + ]; + } + + /** + * Get the error messages for the defined validation rules. + */ + public function messages() + { + return [ + 'required' => ':attribute tidak boleh kosong.', + 'string' => ':attribute harus berupa string.', + 'numeric' => ':attribute harus berupa angka.', + 'digits' => ':attribute harus berjumlah :digits digit.', + 'digits_between' => ':attribute harus berjumlah antara :min sampai :max digit.', + 'email' => ':attribute harus berupa email.', + 'date' => ':attribute harus berupa tanggal.', + 'in' => ':attribute harus salah satu dari jenis berikut :values.', + 'max' => ':attribute maksimal berjumlah :max karakter.', + 'min' => ':attribute minimal berjumlah :min karakter.', + 'regex' => ':attribute hanya boleh berisi huruf.', ]; } } diff --git a/resources/views/layouts/main.blade.php b/resources/views/layouts/main.blade.php index 4e1899d..64e7a66 100644 --- a/resources/views/layouts/main.blade.php +++ b/resources/views/layouts/main.blade.php @@ -11,6 +11,17 @@ SB Admin 2 - Dashboard @include('layouts.link') +{{-- --}} @stack('style') @@ -29,6 +40,26 @@
+ + {{-- @if (session()->has('success')) +
+ {{ session()->get('success') }} +
+ @elseif(session()->has('error')) +
+ X {{ session()->get('error') }} +
+ @endif --}} + @if (session()->has('success')) +
+ {{ session()->get('success') }} +
+ @elseif(session()->has('error')) +
+ X {{ session()->get('error') }} +
+ @endif + @yield('content')
@@ -83,6 +114,18 @@ @include('layouts.script') + + + +