Browse Source

keempat

master
Gunawan19621 1 year ago
parent
commit
cbcc053ede
  1. 72
      app/Http/Controllers/ProfileController.php
  2. 33
      app/Http/Requests/UpdatePenggunaRequest.php
  3. 43
      resources/views/layouts/main.blade.php
  4. 4
      resources/views/profil/profil.blade.php
  5. 10
      routes/web.php

72
app/Http/Controllers/ProfileController.php

@ -29,39 +29,67 @@ class ProfileController extends Controller
} }
//Proses update Profile //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 { try {
$pengguna = User::findOrFail($id); $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) { } catch (\Throwable $th) {
dd($th); // Uncomment this line to see the error details
return redirect()->route('pengguna.index')->with('error', 'Data pengguna gagal diubah'); // dd($th);
return redirect()->back()->with('error', 'Data pengguna gagal diubah');
} }
} }
// public function updateprofile(Request $request)
// { // {
// // dd('oke'); // // dd('oke');
// $messages = [
// 'tgl_lahir.date_format' => 'Format tanggal lahir harus sesuai dengan d/m/Y.',
// ];
// try {
// $request->validate([ // $request->validate([
// 'name' => 'required', // 'name' => 'required|string|max:255|min:3|regex:/^[a-zA-Z\s]+$/',
// 'email' => 'required', // 'email' => 'required|email|max:255|min:3',
// 'phone' => 'required', // 'phone' => 'required|numeric|digits_between:10,13',
// 'foto' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
// 'tgl_lahir' => 'date', // 'tgl_lahir' => 'date',
// 'jenis_kelamin' => 'required', // 'jenis_kelamin' => 'required|in:L,P',
// 'agama' => 'required', // 'agama' => 'required|in:Islam,Kristen,Katolik,Hindu,Budha,Konghucu',
// 'alamat' => 'required', // 'alamat' => 'required|string|max:255|min:3',
// ], $messages); // ]);
// // dd($request->all()); // // dd($request);
// return back()->with('success', 'Profil berhasil di update.'); // try {
// $pengguna = User::findOrFail($id);
// $pengguna->update($request->validated());
// return redirect()->back()->with('success', 'Profil pengguna berhasil diperbarui');
// } catch (\Throwable $th) { // } catch (\Throwable $th) {
// // dd($th); // dd($th);
// return back()->with('eror', 'Profil gagal di update.'); // return redirect()->back()->with('error', 'Data pengguna gagal diubah');
// } // }
// } // }

33
app/Http/Requests/UpdatePenggunaRequest.php

@ -11,18 +11,45 @@ class UpdatePenggunaRequest extends FormRequest
*/ */
public function authorize(): bool public function authorize(): bool
{ {
return false; return true;
} }
/** /**
* Get the validation rules that apply to the request. * Get the validation rules that apply to the request.
* *
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string> * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/ */
public function rules(): array public function rules(): array
{ {
return [ 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.',
]; ];
} }
} }

43
resources/views/layouts/main.blade.php

@ -11,6 +11,17 @@
<title>SB Admin 2 - Dashboard</title> <title>SB Admin 2 - Dashboard</title>
@include('layouts.link') @include('layouts.link')
</head> </head>
{{-- <style>
/* CSS untuk membuat alert mengambang */
.floating-alert {
position: fixed;
/* height: 100%; */
width: 80%;
top: 100px;
right: 30px;
z-index: 9999;
}
</style> --}}
<body id="page-top"> <body id="page-top">
@stack('style') @stack('style')
@ -29,6 +40,26 @@
<!-- Begin Page Content --> <!-- Begin Page Content -->
<div class="container-fluid"> <div class="container-fluid">
<!-- allert update data foto -->
{{-- @if (session()->has('success'))
<div id="success-alert" class="alert alert-success floating-alert">
{{ session()->get('success') }}
</div>
@elseif(session()->has('error'))
<div id="error-alert" class="alert alert-danger floating-alert">
X {{ session()->get('error') }}
</div>
@endif --}}
@if (session()->has('success'))
<div id="success-alert" class="alert alert-success">
{{ session()->get('success') }}
</div>
@elseif(session()->has('error'))
<div id="success-alert" class="alert alert-danger">
X {{ session()->get('error') }}
</div>
@endif
<!-- allert update data foto -->
@yield('content') @yield('content')
</div> </div>
<!-- /.container-fluid --> <!-- /.container-fluid -->
@ -83,6 +114,18 @@
@include('layouts.script') @include('layouts.script')
<!-- Batas waktu alert -->
<script>
// Hapus alert setelah 3 detik
setTimeout(function() {
var successAlert = document.getElementById('success-alert');
if (successAlert) {
successAlert.remove();
}
}, 3000); // 3000 milidetik = 3 detik
</script>
<!-- Batas waktu alert -->
<!-- update data foto --> <!-- update data foto -->
<script> <script>
$(document).ready(function() { $(document).ready(function() {

4
resources/views/profil/profil.blade.php

@ -3,8 +3,10 @@
@section('content') @section('content')
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<form action="{{ route('update.updateprofile') }}" method="POST" enctype="multipart/form-data"> <form action="{{ route('profile.updateprofile', auth()->user()->id) }}" method="POST"
enctype="multipart/form-data">
@csrf @csrf
@method('PUT')
<div class="row"> <div class="row">
<div class="col-9"> <div class="col-9">
<label for="name" class="col-form-label" style="font-size: 14px;">Nama</label> <label for="name" class="col-form-label" style="font-size: 14px;">Nama</label>

10
routes/web.php

@ -23,18 +23,12 @@ Route::get('/', function () {
return view('welcome'); return view('welcome');
}); });
// Route::get('/dashboard', function () {
// return view('dashboard');
// })->middleware(['auth', 'verified'])->name('dashboard');
// Route::get('/dashboard2', function () {
// return view('dashboard.index');
// })->middleware(['auth', 'verified'])->name('dashboard');
Route::middleware('auth')->group(function () { Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit'); Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update'); Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy'); Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
Route::post('/profile/update', [ProfileController::class, 'updateprofile'])->name('update.updateprofile'); Route::put('/profile/{id}', [ProfileController::class, 'updateprofile'])->name('profile.updateprofile');
// Route::put('/profile/{id}', 'ProfileController@updateprofile')->name('profile.update');
Route::post('/profile-update', [ProfileController::class, 'updatePhoto'])->name('profile-update'); Route::post('/profile-update', [ProfileController::class, 'updatePhoto'])->name('profile-update');
Route::get('/setting', [ProfileController::class, 'setting'])->name('profile.setting'); Route::get('/setting', [ProfileController::class, 'setting'])->name('profile.setting');
}); });

Loading…
Cancel
Save