Files
expense/app/Services/AttachmentService.php
T

72 lines
1.8 KiB
PHP
Raw Normal View History

2025-10-13 10:55:21 +07:00
<?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();
}
}