fix(attachment): menambahkan logik penghapusan attachment ketika update data
This commit is contained in:
parent
1a020b3669
commit
7a47675a37
@ -5,6 +5,7 @@
|
|||||||
use App\Models\Attachment;
|
use App\Models\Attachment;
|
||||||
use Illuminate\Http\UploadedFile;
|
use Illuminate\Http\UploadedFile;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
trait UploadAttachment
|
trait UploadAttachment
|
||||||
@ -12,6 +13,17 @@ trait UploadAttachment
|
|||||||
public function uploadAttachment(UploadedFile $file, string $path, string $classModel, string $featureId): void
|
public function uploadAttachment(UploadedFile $file, string $path, string $classModel, string $featureId): void
|
||||||
{
|
{
|
||||||
DB::transaction(function () use ($file, $path, $classModel, $featureId) {
|
DB::transaction(function () use ($file, $path, $classModel, $featureId) {
|
||||||
|
$existingAttachment = Attachment::where('attachmentable_id', $featureId)
|
||||||
|
->where('attachmentable_type', $classModel)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existingAttachment) {
|
||||||
|
$oldFilePath = $path.'/'.$existingAttachment->created_at->format('Y-m-d').'/'.$existingAttachment->name.'.'.$existingAttachment->extension;
|
||||||
|
Storage::disk('public')->delete($oldFilePath);
|
||||||
|
|
||||||
|
$existingAttachment->delete();
|
||||||
|
}
|
||||||
|
|
||||||
$date = now()->format('Y-m-d');
|
$date = now()->format('Y-m-d');
|
||||||
$extension = $file->getClientOriginalExtension();
|
$extension = $file->getClientOriginalExtension();
|
||||||
$fileName = Str::uuid();
|
$fileName = Str::uuid();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user