Siopas Inventory PETI for ISTW Website
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.0 KiB

1 year ago
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\View\View;
use Illuminate\Support\Str;
1 year ago
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
1 year ago
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Storage;
1 year ago
use Illuminate\Support\Facades\Redirect;
use App\Http\Requests\ProfileUpdateRequest;
1 year ago
use App\Http\Requests\UpdatePenggunaRequest;
1 year ago
class ProfileController extends Controller
{
//halaman setting profile
public function setting(Request $request): View
{
$data = [
'active' => 'menu-profile',
];
1 year ago
return view('profil.setting', [
'user' => $request->user(),
], $data);
1 year ago
}
// Halaman edit profile
public function edit()
{
$data = [
'active' => 'menu-profile',
];
return view('profil.profil', $data);
1 year ago
}
1 year ago
//Proses update Profile
1 year ago
public function updateprofile(Request $request, $id)
1 year ago
{
1 year ago
$request->validate([
1 year ago
'fullname' => 'required',
'email' => 'required',
'no_hp' => 'required',
'foto' => 'image|mimes:jpeg,png,jpg,gif|max:2048', // Validasi jenis file gambar
'tgl_lahir' => 'required',
'jenis_kelamin' => 'required',
'agama' => 'required',
'address' => 'required',
1 year ago
]);
1 year ago
try {
$pengguna = User::findOrFail($id);
1 year ago
// Handle foto upload if provided
if ($request->hasFile('foto')) {
1 year ago
// Hapus foto lama jika ada
if ($pengguna->foto) {
Storage::delete('public/' . $pengguna->foto);
}
$randomFileName = Str::random(20); // Menghasilkan nama file acak dengan panjang 20 karakter
$fotoPath = $request->file('foto')->storeAs('public/Profile_foto', $randomFileName);
$pengguna->foto = str_replace('public/', '', $fotoPath);
1 year ago
}
// Update other fields
1 year ago
$pengguna->fullname = $request->fullname;
1 year ago
$pengguna->email = $request->email;
1 year ago
$pengguna->no_hp = $request->no_hp;
1 year ago
$pengguna->tgl_lahir = $request->tgl_lahir;
$pengguna->jenis_kelamin = $request->jenis_kelamin;
$pengguna->agama = $request->agama;
1 year ago
$pengguna->address = $request->address;
1 year ago
$pengguna->save();
return redirect()->back()->with('success', 'Profil pengguna berhasil diperbarui');
1 year ago
} catch (\Throwable $th) {
1 year ago
return redirect()->back()->with('error', 'Data pengguna gagal diubah');
1 year ago
}
}
1 year ago
//Proses update Profile Informasi
public function update(ProfileUpdateRequest $request): RedirectResponse
{
$request->user()->fill($request->validated());
1 year ago
if ($request->user()->isDirty('email')) {
$request->user()->email_verified_at = null;
}
1 year ago
$request->user()->save();
1 year ago
return Redirect::back()->with('status', 'profile-updated');
}
1 year ago
}