Wednesday, 19 March 2025

DAY23 Change pwd admin

27 Nov 2024
52
luminox watches

หลังจากที่ copy form update info admin มาใช้ เราก็ต้องมาเปลี่ยน route เพิ่ม

 <form method=”POST” action=”{{route(‘admin.update.password’)}}” class=”forms-sample” enctype=”multipart/form-data”>
จากนั้นก็มาเพิ่ม route ใน web.php
Route::post(‘/admin/update/password’, [AdminController::class, ‘AdminUpdatePassword’])->name(‘admin.update.password’);
จากนั้นมาสร้าง Admin Controller กันต่อ
use Illuminate\Support\Facades\Hash;  //โหลด hash มาใช้

public function AdminUpdatePassword(Request $request){
// Validation
$request->validate([
‘old_password’ => ‘required’,
‘new_password’ => ‘required|confirmed’
]);

// Check if old password matches
if (!Hash::check($request->old_password, auth()->user()->password)) {
$notification = array(
‘message’ => ‘Old Password Does not Match!’,
‘alert-type’ => ‘error’
);
return back()->with($notification);
}

// Update the new password
User::whereId(auth()->user()->id)->update([
‘password’ => Hash::make($request->new_password)
]);

$notification = array(
‘message’ => ‘Password changed successfully!’,
‘alert-type’ => ‘success’
);
return back()->with($notification);
} // end Method

จากนั้นก็ลอง test ดู เย้! วันนี้คุณทำเสร็จแล้ว

test-chg-pwd

test-chg-pwd

Thanat Sirikitphattana

แบ่งปันกัน เราอยู่กันไม่เกิน 100 ปีหรอกครับ
สุดท้ายก็ทิ้งไว้ที่โลก จะคงเหลือไว้แต่คุณงามความดีที่ให้ระลึกถึงกันครับ

Follow Us / Thanat Sirikitphattana