enhanced services attachment

This commit is contained in:
Fiqh Pratama
2025-10-13 10:55:21 +07:00
parent 65d176e6b2
commit dd5dbcab8f
9 changed files with 184 additions and 0 deletions
+71
View File
@@ -0,0 +1,71 @@
<?php
namespace App\Services;
use App\Models\AttachmentForm;
use Illuminate\Support\Facades\DB;
class AttachmentService
{
/**
* Store single attachment
*/
public function addAttachment(int $formId, string $tableName, string $filePath, ?string $category = null): AttachmentForm
{
return AttachmentForm::create([
'form_id' => $formId,
'table_name' => $tableName,
'file_category'=> $category,
'file_path' => $filePath,
]);
}
/**
* Store multiple attachments
*/
public function addMultipleAttachments(int $formId, string $tableName, array $attachments): void
{
$data = [];
foreach ($attachments as $file) {
$data[] = [
'form_id' => $formId,
'table_name' => $tableName,
'file_category' => $file['file_category'] ?? null,
'file_path' => $file['file_path'],
'created_at' => now(),
'updated_at' => now(),
];
}
AttachmentForm::insert($data);
}
/**
* Get attachments by form
*/
public function getAttachments(int $formId, string $tableName)
{
return AttachmentForm::where('form_id', $formId)
->where('table_name', $tableName)
->get();
}
/**
* Delete attachment by ID (soft delete)
*/
public function deleteAttachment(int $attachmentId): bool
{
return AttachmentForm::where('id', $attachmentId)->delete();
}
/**
* Delete all attachment by form
*/
public function deleteByForm(int $formId, string $tableName): bool
{
return AttachmentForm::where('form_id', $formId)
->where('table_name', $tableName)
->delete();
}
}