feat: trait untuk handle upload attachment
This commit is contained in:
parent
15b5f7ae0d
commit
ec0d01e683
29
app/Traits/UploadAttachment.php
Normal file
29
app/Traits/UploadAttachment.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Attachment;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait UploadAttachment
|
||||
{
|
||||
public function uploadAttachment(UploadedFile $file, string $path, string $classModel, string $featureId): void
|
||||
{
|
||||
DB::transaction(function () use ($file, $path, $classModel, $featureId) {
|
||||
$date = now()->format('Y-m-d');
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
$fileName = Str::uuid();
|
||||
$file->storeAs($path.'/'.$date, $fileName.'.'.$extension, 'public');
|
||||
|
||||
Attachment::create([
|
||||
'attachmentable_id' => $featureId,
|
||||
'attachmentable_type' => $classModel,
|
||||
'name' => $fileName,
|
||||
'extension' => $extension,
|
||||
'size' => $file->getSize(),
|
||||
]);
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user