MIF_E31230356/app/Http/Controllers/Guru/ProfileController.php

54 lines
1.8 KiB
PHP

<?php
namespace App\Http\Controllers\Guru;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
class ProfileController extends Controller
{
public function updateAjax(Request $request)
{
$guru = Auth::guard('guru')->user();
$request->validate([
'password' => 'nullable|min:6|confirmed',
'password_confirmation' => 'nullable',
'foto_profil' => 'nullable|image|mimes:jpg,jpeg,png,webp|max:2048',
], [
'password.min' => 'Password minimal 6 karakter.',
'password.confirmed' => 'Konfirmasi password tidak cocok.',
'foto_profil.image' => 'File harus berupa gambar.',
'foto_profil.max' => 'Ukuran foto maksimal 2MB.',
]);
$data = [];
$fotoUrl = null;
if ($request->filled('password')) {
$data['password'] = Hash::make($request->password);
}
if ($request->hasFile('foto_profil')) {
if ($guru->foto_profil && Storage::disk('public')->exists($guru->foto_profil)) {
Storage::disk('public')->delete($guru->foto_profil);
}
$path = $request->file('foto_profil')->store('foto_profil/guru', 'public');
$data['foto_profil'] = $path;
$fotoUrl = Storage::url($path);
}
if (!empty($data)) {
$guru->update($data);
}
return response()->json([
'success' => true,
'message' => 'Profil berhasil diperbarui!',
'foto_url' => $fotoUrl ?? ($guru->foto_profil ? Storage::url($guru->foto_profil) : null),
]);
}
}