add logic in profil controller and profil view for edit profil
This commit is contained in:
parent
9de4993af6
commit
e07d270b7f
|
|
@ -0,0 +1,83 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator; // <--- WAJIB ADA INI
|
||||
use App\Models\User;
|
||||
|
||||
class ProfilController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$user = Auth::user();
|
||||
return view('admin.profil', compact('user'));
|
||||
}
|
||||
|
||||
// 1. UPDATE INFO DASAR (Tas Error: 'updateProfil')
|
||||
public function update(Request $request)
|
||||
{
|
||||
$user = User::find(Auth::id());
|
||||
|
||||
// Aturan Validasi
|
||||
$validator = Validator::make($request->all(), [
|
||||
'nama' => 'required|string|max:100',
|
||||
// Pastikan ignore ID user yang benar (id_user atau id)
|
||||
'username' => 'required|string|alpha_num|max:50|unique:users,username,' . $user->id_user . ',id_user',
|
||||
'email' => 'required|email|max:255|unique:users,email,' . $user->id_user . ',id_user',
|
||||
'no_wa' => 'nullable|numeric',
|
||||
'alamat' => 'nullable|string|max:255',
|
||||
], [
|
||||
'required' => 'Kolom :attribute wajib diisi.',
|
||||
'unique' => ':attribute sudah digunakan.',
|
||||
'numeric' => ':attribute harus berupa angka.',
|
||||
], [
|
||||
'nama' => 'Nama Lengkap',
|
||||
'no_wa' => 'Nomor WA'
|
||||
]);
|
||||
|
||||
// CEK ERROR KHUSUS TAS 'updateProfil'
|
||||
if ($validator->fails()) {
|
||||
// Kita lempar error ke tas 'updateProfil'
|
||||
return back()->withErrors($validator, 'updateProfil')->withInput();
|
||||
}
|
||||
|
||||
// Simpan Data
|
||||
$user->nama = $request->nama;
|
||||
$user->username = $request->username;
|
||||
$user->email = $request->email;
|
||||
$user->no_wa = $request->no_wa;
|
||||
$user->alamat = $request->alamat;
|
||||
$user->save();
|
||||
|
||||
return back()->with('success', 'Profil berhasil diperbarui!');
|
||||
}
|
||||
|
||||
// 2. UPDATE PASSWORD (Tas Error: 'updatePassword')
|
||||
public function updatePassword(Request $request)
|
||||
{
|
||||
// Aturan Validasi
|
||||
$validator = Validator::make($request->all(), [
|
||||
'password' => 'required|min:8|confirmed', // confirmed cek field password_confirmation
|
||||
], [
|
||||
'required' => 'Kata sandi baru wajib diisi.',
|
||||
'min' => 'Kata sandi minimal :min karakter.',
|
||||
'confirmed' => 'Konfirmasi kata sandi tidak cocok.',
|
||||
]);
|
||||
|
||||
if ($validator->fails()) {
|
||||
// Tambahkan ->withInput()
|
||||
return back()->withErrors($validator, 'updatePassword')->withInput();
|
||||
}
|
||||
|
||||
// Simpan Password
|
||||
$user = User::find(Auth::id());
|
||||
$user->password = Hash::make($request->password);
|
||||
$user->save();
|
||||
|
||||
return back()->with('success', 'Kata sandi berhasil diubah!');
|
||||
}
|
||||
}
|
||||
|
|
@ -2,74 +2,116 @@
|
|||
@section('title', 'Profil')
|
||||
|
||||
@section('content')
|
||||
@if (session('success'))
|
||||
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
||||
{{ session('success') }}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
@endif
|
||||
|
||||
<section id="multiple-column-form">
|
||||
<div class="row match-height">
|
||||
|
||||
<div class="col-12">
|
||||
<h4 class="card-title">Edit Profil</h4>
|
||||
<div class="card">
|
||||
<div class="card-content">
|
||||
<div class="card-body">
|
||||
<form class="form">
|
||||
<form class="form" id="formProfil" action="{{ route('admin.profil.simpan') }}" method="POST">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
|
||||
<div class="col-12">
|
||||
<div class="row gx-1">
|
||||
<div class="row gx-2">
|
||||
<div class="col-12 col-md-6">
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Nama Lengkap</label>
|
||||
<input type="text" class="form-control"
|
||||
style="font-size: 13px;"placeholder="Masukkan Nama Lengkap">
|
||||
<input type="text"
|
||||
class="form-control @error('nama', 'updateProfil') is-invalid @enderror"
|
||||
name="nama" value="{{ old('nama', $user->nama) }}"
|
||||
style="font-size: 13px; padding: 8px;"
|
||||
placeholder="Masukkan Nama Lengkap">
|
||||
|
||||
@error('nama', 'updateProfil')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-12 col-md-6">
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Username</label>
|
||||
<input type="text" class="form-control"
|
||||
style="font-size: 13px;"placeholder="Masukkan Username">
|
||||
<input type="text"
|
||||
class="form-control @error('username', 'updateProfil') is-invalid @enderror"
|
||||
name="username" value="{{ old('username', $user->username) }}"
|
||||
style="font-size: 13px; padding: 8px;" placeholder="Masukkan Username">
|
||||
|
||||
@error('username', 'updateProfil')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row gx-1">
|
||||
|
||||
<div class="row gx-2">
|
||||
<div class="col-12 col-md-4">
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Nomor WA</label>
|
||||
<input type="number" class="form-control"
|
||||
style="font-size: 13px;"placeholder="Masukkan Nomor WA">
|
||||
<input type="number"
|
||||
class="form-control @error('no_wa', 'updateProfil') is-invalid @enderror"
|
||||
name="no_wa" value="{{ old('no_wa', $user->no_wa) }}"
|
||||
style="font-size: 13px; padding: 8px;" placeholder="Masukkan Nomor WA">
|
||||
|
||||
@error('no_wa', 'updateProfil')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-12 col-md-4">
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Email</label>
|
||||
<input type="email" class="form-control"
|
||||
style="font-size: 13px;"placeholder="Masukkan Email">
|
||||
<input type="email"
|
||||
class="form-control @error('email', 'updateProfil') is-invalid @enderror"
|
||||
name="email" value="{{ old('email', $user->email) }}"
|
||||
style="font-size: 13px; padding: 8px;" placeholder="Masukkan Email">
|
||||
|
||||
@error('email', 'updateProfil')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-12 col-md-4">
|
||||
<div class="mb-2">
|
||||
<label class="form-label">Peran</label>
|
||||
|
||||
<fieldset class="form-group">
|
||||
<select class="form-select" id="basicSelect" style="font-size: 13px">
|
||||
<option>Admin Foto</option>
|
||||
<option>Admin Buket</option>
|
||||
</select>
|
||||
</fieldset>
|
||||
<input type="text" class="form-control bg-light"
|
||||
value="{{ ucfirst($user->role ?? 'Admin') }}" style="font-size: 13px;"
|
||||
readonly>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Alamat</label>
|
||||
<input type="text" class="form-control"style="font-size: 13px;"
|
||||
placeholder="Masukkan Alamat Lengkap">
|
||||
<input type="text"
|
||||
class="form-control @error('alamat', 'updateProfil') is-invalid @enderror"
|
||||
name="alamat" value="{{ old('alamat', $user->alamat) }}"
|
||||
style="font-size: 13px;" placeholder="Masukkan Alamat Lengkap">
|
||||
|
||||
@error('alamat', 'updateProfil')
|
||||
<div class="invalid-feedback">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
<div class="col-12 d-flex justify-content-end mt-2 gap-2">
|
||||
|
||||
<button type="button" class="btn btn-light batal rounded-pill py-2 px-4">
|
||||
<button type="reset" class="btn btn-light batal rounded-pill py-2 px-4">
|
||||
Batal
|
||||
</button>
|
||||
|
||||
<button type="button" class="btn btn-primary kirim-wa rounded-pill py-2 px-4">
|
||||
<button type="submit" form="formProfil"
|
||||
class="btn btn-primary kirim-wa rounded-pill py-2 px-4">
|
||||
Simpan
|
||||
</button>
|
||||
|
||||
|
|
@ -79,39 +121,59 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-12">
|
||||
<h4 class="card-title">Ubah Kata Sandi</h4>
|
||||
<div class="card">
|
||||
<div class="card-content">
|
||||
<div class="card-body">
|
||||
<form class="form">
|
||||
<form class="form" id="formPassword" action="{{ route('admin.profil.password') }}"
|
||||
method="POST">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
|
||||
<div class="row">
|
||||
<div class="col-12 col-md-6">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Kata Sandi Baru</label>
|
||||
<div class="form-group position-relative has-icon-right">
|
||||
<input type="password" class="form-control" id="passBaru"
|
||||
placeholder="**************">
|
||||
|
||||
<div class="form-control-icon" style="cursor: pointer;"
|
||||
onclick="toggleDynamic('passBaru', 'iconBaru')">
|
||||
<i class="bi bi-eye" style="font-size: 16px" id="iconBaru"></i>
|
||||
</div>
|
||||
<div class="position-relative">
|
||||
<input type="password"
|
||||
class="form-control @error('password', 'updatePassword') is-invalid @enderror"
|
||||
id="passBaru" name="password" placeholder="**************"
|
||||
style="padding-right: 40px; font-size: 13px;">
|
||||
|
||||
<span class="position-absolute top-50 end-0 translate-middle-y me-3"
|
||||
onclick="toggleDynamic('passBaru', 'iconBaru')"
|
||||
style="cursor: pointer;">
|
||||
<i class="bi bi-eye text-secondary" style="font-size: 13px"
|
||||
id="iconBaru"></i>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
@error('password', 'updatePassword')
|
||||
<div class="invalid-feedback d-block">{{ $message }}</div>
|
||||
@enderror
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-12 col-md-6">
|
||||
<div class="mb-3">
|
||||
<label class="form-label">Konfirmasi Kata Sandi Baru</label>
|
||||
<div class="form-group position-relative has-icon-right">
|
||||
<input type="password" class="form-control" id="passKonfirm"
|
||||
placeholder="**************">
|
||||
|
||||
<div class="form-control-icon" style="cursor: pointer;"
|
||||
onclick="toggleDynamic('passKonfirm', 'iconKonfirm')">
|
||||
<i class="bi bi-eye" style="font-size: 16px" id="iconKonfirm"></i>
|
||||
</div>
|
||||
<div class="position-relative">
|
||||
<input type="password"
|
||||
class="form-control @error('password', 'updatePassword') is-invalid @enderror"
|
||||
id="passKonfirm" name="password_confirmation"
|
||||
placeholder="**************"
|
||||
style="padding-right: 40px; font-size: 13px">
|
||||
|
||||
<span class="position-absolute top-50 end-0 translate-middle-y me-3"
|
||||
onclick="toggleDynamic('passKonfirm', 'iconKonfirm')"
|
||||
style="cursor: pointer;">
|
||||
<i class="bi bi-eye text-secondary" style="font-size: 13px"
|
||||
id="iconKonfirm"></i>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -119,25 +181,26 @@
|
|||
|
||||
<div class="col-12 d-flex justify-content-end mt-2 gap-2">
|
||||
|
||||
<button type="button" class="btn btn-light batal rounded-pill py-2 px-4">
|
||||
<button type="reset" class="btn btn-light batal rounded-pill py-2 px-4">
|
||||
Batal
|
||||
</button>
|
||||
|
||||
<button type="button" class="btn btn-primary kirim-wa rounded-pill py-2 px-4">
|
||||
<button type="submit" form="formPassword"
|
||||
class="btn btn-primary kirim-wa rounded-pill py-2 px-4">
|
||||
Simpan
|
||||
</button>
|
||||
|
||||
</div>
|
||||
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<script>
|
||||
// Nama fungsi saya ganti jadi toggleDynamic biar ga bentrok
|
||||
function toggleDynamic(inputId, iconId) {
|
||||
const passwordInput = document.getElementById(inputId);
|
||||
const eyeIcon = document.getElementById(iconId);
|
||||
|
|
|
|||
Loading…
Reference in New Issue