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
+26
View File
@@ -0,0 +1,26 @@
<?php
namespace App\Enums;
class AttachmentTableName
{
const FORM_MEETING_SEMINAR = 'form_meeting_seminar';
const FORM_OTHERS = 'form_others';
const FORM_UP_COUNTRY = 'form_up_country';
const FORM_VEHICLE_RUNNING_COST = 'form_vehicle_running_cost';
const FORM_ENTERTAINMENT_PRESENTATION = 'form_entertainment_presentation';
/**
* Untuk validasi input atau dropdown category table_name
*/
public static function all(): array
{
return [
self::FORM_MEETING_SEMINAR,
self::FORM_OTHERS,
self::FORM_UP_COUNTRY,
self::FORM_VEHICLE_RUNNING_COST,
self::FORM_ENTERTAINMENT_PRESENTATION,
];
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class AttachmentForm extends Model
{
use SoftDeletes;
protected $table = 'attachment_form';
protected $fillable = [
'form_id',
'table_name',
'file_category',
'file_path',
];
/**
* Relasi dinamis ke table form terkait
*/
public function form()
{
return $this->morphTo(__FUNCTION__, 'table_name', 'form_id');
}
}
@@ -37,4 +37,11 @@ class FormEntertaimentPresentation extends Model
{ {
return $this->belongsTo('App\Models\Admin'); return $this->belongsTo('App\Models\Admin');
} }
public function attachments()
{
return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id')
->where('table_name', \App\Enums\AttachmentTableName::FORM_ENTERTAINMENT_PRESENTATION);
}
} }
+6
View File
@@ -40,4 +40,10 @@ class FormMeetingSeminar extends Model
{ {
return $this->belongsTo(Admin::class, 'user_id'); return $this->belongsTo(Admin::class, 'user_id');
} }
public function attachments()
{
return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id')
->where('table_name', \App\Enums\AttachmentTableName::FORM_MEETING_SEMINAR);
}
} }
+6
View File
@@ -39,4 +39,10 @@ class FormOthers extends Model
{ {
return $this->belongsTo(Kategori::class); return $this->belongsTo(Kategori::class);
} }
public function attachments()
{
return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id')
->where('table_name', \App\Enums\AttachmentTableName::FORM_OTHERS);
}
} }
+6
View File
@@ -52,4 +52,10 @@ class FormUpCountry extends Model
{ {
return $this->belongsTo('App\Models\Rayon'); return $this->belongsTo('App\Models\Rayon');
} }
public function attachments()
{
return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id')
->where('table_name', \App\Enums\AttachmentTableName::FORM_UP_COUNTRY);
}
} }
+6
View File
@@ -38,4 +38,10 @@ class FormVehicleRunningCost extends Model
{ {
return $this->belongsTo('App\Models\Admin', 'user_id'); return $this->belongsTo('App\Models\Admin', 'user_id');
} }
public function attachments()
{
return $this->hasMany(\App\Models\AttachmentForm::class, 'form_id')
->where('table_name', \App\Enums\AttachmentTableName::FORM_VEHICLE_RUNNING_COST);
}
} }
+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();
}
}
@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('attachment_form', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('form_id');
$table->string('table_name', 100)->comment('nama tabel form yang direferensikan');
$table->string('file_category', 100)->nullable();
$table->string('file_path', 255);
$table->timestamps();
$table->softDeletes();
$table->index(['form_id', 'table_name'], 'idx_form_table');
});
}
public function down(): void
{
Schema::dropIfExists('attachment_form');
}
};