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

View File

@ -29,13 +29,13 @@
<div class="card-body"> <div class="card-body">
<table class="table table-striped" id="table1"> <table class="table table-striped" id="table1">
<thead> <thead>
<tr> <tr class="text-center">
<th style="width: 5%">No.</th> <th style="width: 5%">No.</th>
<th style="width: 20%">Nama Buket</th> <th style="width: 20%">Nama Buket</th>
<th style="width: 35%">Deskripsi</th> <th style="width: 35%">Deskripsi</th>
<th style="width: 15%" class="text-nowrap">Harga</th> <th style="width: 15%" class="text-nowrap">Harga</th>
<th style="width: 10%">Foto</th> <th style="width: 10%">Foto</th>
<th style="width: 15%" class="text-center">Aksi</th> <th style="width: 15%">Aksi</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@ -46,7 +46,7 @@
<td style="width: 35%">{{ Str::limit($b->deskripsi, 50) }}</td> <td style="width: 35%">{{ Str::limit($b->deskripsi, 50) }}</td>
<td style="width: 15%">Rp {{ number_format($b->harga, 0, ',', '.') }}</td> <td style="width: 15%">Rp {{ number_format($b->harga, 0, ',', '.') }}</td>
<td style="width:10%"> <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;"> style="width: 50px; height: 50px; object-fit: cover;">
</td> </td>
<td class="col-auto text-center" style="width: 15%"> <td class="col-auto text-center" style="width: 15%">
@ -71,7 +71,7 @@
@include('admin.produk-buket.partials.modal-delete') @include('admin.produk-buket.partials.modal-delete')
@empty @empty
<tr> <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> </tr>
@endforelse @endforelse
</tbody> </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> <p class="mb-0" style="font-size: 12px;">Belum ada foto</p>
</div> </div>
<img id="editImgPreview{{ $b->id_buket }}" src="{{ asset($b->foto) }}" <img id="editImgPreview{{ $b->id_buket }}"
class="w-100 h-100" style="object-fit: contain;"> src="{{ asset('storage/' . $b->foto) }}" class="w-100 h-100"
style="object-fit: contain;">
</div> </div>
</div> </div>
</div> </div>

View File

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