Browse Source

keempat

master
Gunawan19621 1 year ago
parent
commit
cbcc053ede
  1. 74
      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

74
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');
// }
// }

33
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<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
* @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
*/
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.',
];
}
}

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

@ -11,6 +11,17 @@
<title>SB Admin 2 - Dashboard</title>
@include('layouts.link')
</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">
@stack('style')
@ -29,6 +40,26 @@
<!-- Begin Page Content -->
<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')
</div>
<!-- /.container-fluid -->
@ -83,6 +114,18 @@
@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 -->
<script>
$(document).ready(function() {

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

@ -3,8 +3,10 @@
@section('content')
<div class="card">
<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
@method('PUT')
<div class="row">
<div class="col-9">
<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');
});
// 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::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
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::get('/setting', [ProfileController::class, 'setting'])->name('profile.setting');
});

Loading…
Cancel
Save