JezK
Edit File: GalleryService.php
<?php namespace App\Services; use App\Models\Gallery; use Illuminate\Database\Eloquent\Collection; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Facades\Storage; use Illuminate\Http\UploadedFile; class GalleryService { public function getAllPaginated(int $perPage = 10): LengthAwarePaginator { return Gallery::with('galleryCategory')->latest()->paginate($perPage); } public function getActiveByType(string $type, int $perPage = 20): LengthAwarePaginator { return Gallery::with('galleryCategory')->active()->where('type', $type)->latest()->paginate($perPage); } public function create(array $data): Gallery { if (isset($data['type']) && $data['type'] === 'photo') { if (isset($data['photo_source_type']) && $data['photo_source_type'] === 'url') { $data['image_path'] = $data['image_url_input'] ?? null; } elseif (isset($data['image']) && $data['image'] instanceof UploadedFile) { $data['image_path'] = $data['image']->store('galleries', 'public'); } } return Gallery::create($data); } public function update(Gallery $gallery, array $data): Gallery { if (isset($data['type']) && $data['type'] === 'photo') { if (isset($data['photo_source_type']) && $data['photo_source_type'] === 'url') { if ($gallery->image_path && !str_starts_with($gallery->image_path, 'http://') && !str_starts_with($gallery->image_path, 'https://')) { Storage::disk('public')->delete($gallery->image_path); } $data['image_path'] = $data['image_url_input'] ?? null; } elseif (isset($data['image']) && $data['image'] instanceof UploadedFile) { if ($gallery->image_path && !str_starts_with($gallery->image_path, 'http://') && !str_starts_with($gallery->image_path, 'https://')) { Storage::disk('public')->delete($gallery->image_path); } $data['image_path'] = $data['image']->store('galleries', 'public'); } } $gallery->update($data); return $gallery->fresh(); } public function delete(Gallery $gallery): bool { if ($gallery->image_path) { Storage::disk('public')->delete($gallery->image_path); } return $gallery->delete(); } }