<?php
namespace App\Http\Controllers;
use App\Models\users;
use Exception;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Hash;
class AdminController extends Controller
{
public function show(String $id)
{
$decryptedId = Crypt::decryptString($id);
$data['admin'] = User::where('id', $decryptedId)->first();
return view('admin.adminAccount.edit')->with($data);
}
public function update(String $id, Request $request)
{
DB::beginTransaction();
try {
// Decrypt the ID
$decryptedId = Crypt::decryptString($id);
// Validate the request
$request->validate([
'username' => 'required',
'email' => 'required|email',
'password' => 'nullable|min:6',
'password_confirm' => 'nullable|same:password',
'photo' => 'nullable|mimes:jpg,jpeg,png|max:300000',
], [
'photo.max' => 'Foto tidak boleh lebih dari 300MB!',
'password.min' => 'Password tidak boleh kurang dari 6 karakter.',
]);
// Prepare data for update
$data = [
'username' => $request->username,
'email' => $request->email,
];
// Check if the password needs to be updated
if (!empty($request->password)) {
$data['password'] = Hash::make($request->password);
}
// Handle the photo upload
if ($request->hasFile('photo')) {
$foto_file = $request->file('photo');
$foto_ekstensi = $foto_file->extension();
$foto_baru = "operator-" . date('ymdhis') . ".$foto_ekstensi";
$foto_file->move(public_path('images/operator'), $foto_baru);
$data['photo'] = $foto_baru;
// Delete the old photo
$getfoto = Users::where('id', $decryptedId)->first();
if ($getfoto && $getfoto->photo) {
File::delete(public_path('images/operator') . "/" . $getfoto->photo);
}
}
// Update the user
$update = Users::where('id', $decryptedId)->update($data);
DB::commit();
if ($update) {
return redirect()->back()->with('success', 'Data berhasil diedit.');
} else {
return redirect()->back()->with('error', 'Data gagal diedit.');
}
} catch (Exception $e) {
DB::rollBack();
return redirect()->back()->with('error', 'Terjadi kesalahan: ' . $e->getMessage());
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]