34 lines
1014 B
PHP
34 lines
1014 B
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Modules\Document\Models\Document;
|
|
|
|
trait AddToDocumentCollection
|
|
{
|
|
public function addToDocumentCollection(string $collectionName = 'uploads', string $file, ?string $documentName = null, ?int $referenceDocumentId = null)
|
|
{
|
|
if (!Storage::disk('public')->exists($collectionName)) {
|
|
Storage::disk('public')->makeDirectory($collectionName);
|
|
}
|
|
|
|
$targetFile = Storage::disk('public')->path("{$collectionName}/{$file}");
|
|
|
|
File::copy(storage_path("tmp/uploads/{$file}"), $targetFile);
|
|
|
|
$this->documents()->create([
|
|
'title' => $documentName,
|
|
'file_path' => $file,
|
|
'collection_name' => $collectionName,
|
|
]);
|
|
}
|
|
|
|
public function documents(): MorphMany
|
|
{
|
|
return $this->morphMany(Document::class, 'documentable')->orderBy('order');
|
|
}
|
|
}
|