orderBy('created_at', 'desc') ->get(); } public function findById(int $id): ?UnitMedia { return UnitMedia::find($id); } public function create(array $data): UnitMedia { $file = $data['file']; $title = $data['title']; $fileUrl = $file->store('unit-media'); $mimeType = $file->getMimeType(); return UnitMedia::create([ 'unit_id' => $data['unit_id'], 'title' => $title, 'file_url' => $fileUrl, 'mime_type' => $mimeType, 'visible_to_franchisee' => $data['visible_to_franchisee'] ?? false, ]); } public function update(int $id, array $data): ?UnitMedia { $model = $this->findById($id); if (!$model) { return null; } if (isset($data['file'])) { Storage::delete($model->file_url); $file = $data['file']; $data['file_url'] = $file->store('unit-media'); $data['mime_type'] = $file->getMimeType(); unset($data['file']); } $model->update($data); return $model->fresh(); } public function delete(int $id): bool { $model = $this->findById($id); if (!$model) { return false; } Storage::delete($model->file_url); return $model->delete(); } }