refactor: update file handling to use Storage facade and improve image paths

This commit is contained in:
LailaWulandarii 2026-01-08 19:00:09 +07:00
parent eb5e2da49f
commit a21939e362
4 changed files with 16 additions and 19 deletions

View File

@ -6,7 +6,7 @@
use App\Models\Buket;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
class BuketController extends Controller
{
@ -59,10 +59,7 @@ public function store(Request $request)
if ($request->hasFile('foto')) {
$file = $request->file('foto');
$filename = time() . '_' . $file->getClientOriginalName();
$file->move(public_path('img/buket'), $filename);
$path = 'img/buket/' . $filename;
$path = $file->storeAs('img/buket', $filename, 'public');
}
Buket::create([
@ -121,16 +118,15 @@ public function update(Request $request, string $id)
$data = $request->only(['nama', 'ukuran', 'kategori', 'harga', 'request_khusus', 'deskripsi']);
if ($request->hasFile('foto')) {
// 1. Hapus foto lama jika ada
if ($buket->foto) {
File::delete(public_path($buket->foto));
Storage::disk('public')->delete($buket->foto);
}
// 2. Upload foto baru
$file = $request->file('foto');
$filename = time() . '_' . $file->getClientOriginalName();
$file->move(public_path('img/buket'), $filename);
$data['foto'] = 'img/buket/' . $filename;
$path = $file->storeAs('img/buket', $filename, 'public');
$data['foto'] = $path;
}
$buket->update($data);
@ -143,7 +139,7 @@ public function destroy(string $id)
$buket = Buket::findOrFail($id);
if ($buket->foto) {
File::delete(public_path($buket->foto));
Storage::disk('public')->delete($buket->foto);
}
$buket->delete();

View File

@ -29,13 +29,13 @@
<div class="card-body">
<table class="table table-striped" id="table1">
<thead>
<tr>
<tr class="text-center">
<th style="width: 5%">No.</th>
<th style="width: 20%">Nama Buket</th>
<th style="width: 35%">Deskripsi</th>
<th style="width: 15%" class="text-nowrap">Harga</th>
<th style="width: 10%">Foto</th>
<th style="width: 15%" class="text-center">Aksi</th>
<th style="width: 15%">Aksi</th>
</tr>
</thead>
<tbody>
@ -46,7 +46,7 @@
<td style="width: 35%">{{ Str::limit($b->deskripsi, 50) }}</td>
<td style="width: 15%">Rp {{ number_format($b->harga, 0, ',', '.') }}</td>
<td style="width:10%">
<img src="{{ asset($b->foto) }}" alt="Foto Produk" class="rounded"
<img src="{{ asset('storage/' . $b->foto) }}" alt="Foto Produk" class="rounded"
style="width: 50px; height: 50px; object-fit: cover;">
</td>
<td class="col-auto text-center" style="width: 15%">
@ -71,7 +71,7 @@
@include('admin.produk-buket.partials.modal-delete')
@empty
<tr>
<td colspan="4" class="text-center text-muted">Tidak ada data buket.</td>
<td colspan="6" class="text-center text-muted">Tidak ada data buket.</td>
</tr>
@endforelse
</tbody>

View File

@ -123,8 +123,9 @@ class="form-control @error('nama') is-invalid @enderror" style="font-size: 14px;
<p class="mb-0" style="font-size: 12px;">Belum ada foto</p>
</div>
<img id="editImgPreview{{ $b->id_buket }}" src="{{ asset($b->foto) }}"
class="w-100 h-100" style="object-fit: contain;">
<img id="editImgPreview{{ $b->id_buket }}"
src="{{ asset('storage/' . $b->foto) }}" class="w-100 h-100"
style="object-fit: contain;">
</div>
</div>
</div>

View File

@ -12,8 +12,8 @@
<div class="col-12 col-sm-4">
@if ($b->foto)
{{-- Langsung img tanpa wrapper --}}
<img src="{{ asset($b->foto) }}" class="custom-img-box"
onclick="showImage('{{ asset($b->foto) }}')">
<img src="{{ asset('storage/' . $b->foto) }}" class="custom-img-box"
onclick="showImage('{{ asset('storage/' . $b->foto) }}')">
@else
{{-- Div pengganti kalau tidak ada foto --}}
<div class="custom-img-box d-flex align-items-center justify-content-center text-muted">