77 lines
2.1 KiB
PHP
77 lines
2.1 KiB
PHP
<?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\File;
|
|
|
|
class ProfilController extends Controller
|
|
{
|
|
public function index()
|
|
{
|
|
$admin = Auth::user();
|
|
return view('admin.profil', compact('admin'));
|
|
}
|
|
|
|
public function update(Request $request)
|
|
{
|
|
$admin = Auth::user();
|
|
|
|
$request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'username' => 'required|string|max:100',
|
|
'foto' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
|
|
'password' => 'nullable|min:6|confirmed',
|
|
]);
|
|
|
|
// ======================
|
|
// UPDATE DATA DASAR
|
|
// ======================
|
|
$admin->name = $request->name;
|
|
$admin->username = $request->username;
|
|
|
|
// ======================
|
|
// PROSES FOTO
|
|
// ======================
|
|
if ($request->hasFile('foto')) {
|
|
|
|
$path = public_path('assets/admin/foto-admin');
|
|
|
|
// buat folder kalau belum ada
|
|
if (!File::exists($path)) {
|
|
File::makeDirectory($path, 0755, true);
|
|
}
|
|
|
|
// hapus foto lama
|
|
if ($admin->foto && File::exists($path.'/'.$admin->foto)) {
|
|
File::delete($path.'/'.$admin->foto);
|
|
}
|
|
|
|
$file = $request->file('foto');
|
|
$namaFoto = time().'_'.uniqid().'.'.$file->getClientOriginalExtension();
|
|
|
|
// SIMPAN FOTO (INI KUNCI)
|
|
$file->move($path, $namaFoto);
|
|
|
|
// simpan ke DB
|
|
$admin->foto = $namaFoto;
|
|
}
|
|
|
|
// ======================
|
|
// PASSWORD
|
|
// ======================
|
|
if ($request->filled('password')) {
|
|
$admin->password = Hash::make($request->password);
|
|
}
|
|
|
|
$admin->save();
|
|
|
|
return redirect()
|
|
->route('admin.profil')
|
|
->with('success', 'Profil berhasil diperbarui');
|
|
}
|
|
}
|