change media delete format..
This commit is contained in:
parent
eaab52328b
commit
d82c251085
@ -37,7 +37,7 @@ class UpdateTransaction extends Job
|
||||
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$this->transaction->delete_attachment();
|
||||
$this->deleteMediaModel($this->transaction, 'attachment', $this->request);
|
||||
|
||||
foreach ($this->request->file('attachment') as $attachment) {
|
||||
$media = $this->getMedia($attachment, 'transactions');
|
||||
@ -45,7 +45,7 @@ class UpdateTransaction extends Job
|
||||
$this->transaction->attachMedia($media, 'attachment');
|
||||
}
|
||||
} elseif (!$this->request->file('attachment') && $this->transaction->attachment) {
|
||||
$this->transaction->delete_attachment();
|
||||
$this->deleteMediaModel($this->transaction, 'attachment', $this->request);
|
||||
}
|
||||
|
||||
// Recurring
|
||||
|
@ -46,7 +46,7 @@ class UpdateDocument extends Job
|
||||
\DB::transaction(function () {
|
||||
// Upload attachment
|
||||
if ($this->request->file('attachment')) {
|
||||
$this->document->delete_attachment();
|
||||
$this->deleteMediaModel($this->document, 'attachment', $this->request);
|
||||
|
||||
foreach ($this->request->file('attachment') as $attachment) {
|
||||
$media = $this->getMedia($attachment, Str::plural($this->document->type));
|
||||
@ -54,7 +54,7 @@ class UpdateDocument extends Job
|
||||
$this->document->attachMedia($media, 'attachment');
|
||||
}
|
||||
} elseif (!$this->request->file('attachment') && $this->document->attachment) {
|
||||
$this->document->delete_attachment();
|
||||
$this->deleteMediaModel($this->document, 'attachment', $this->request);
|
||||
}
|
||||
|
||||
$this->deleteRelationships($this->document, ['items', 'item_taxes', 'totals']);
|
||||
|
@ -3,10 +3,10 @@
|
||||
namespace App\Traits;
|
||||
|
||||
use MediaUploader;
|
||||
use App\Models\Common\Media as MediaModel;
|
||||
|
||||
trait Uploads
|
||||
{
|
||||
|
||||
public function getUploadedFilePath($file, $folder = 'settings', $company_id = null)
|
||||
{
|
||||
$path = '';
|
||||
@ -63,4 +63,35 @@ trait Uploads
|
||||
|
||||
return MediaUploader::importPath($disk, $path);
|
||||
}
|
||||
|
||||
public function deleteMediaModel($model, $parameter, $request = null)
|
||||
{
|
||||
$medias = $model->$parameter;
|
||||
|
||||
if (!$medias) {
|
||||
return;
|
||||
}
|
||||
|
||||
$already_uploaded = [];
|
||||
|
||||
if ($request && isset($request['uploaded_' . $parameter])) {
|
||||
$uploaded = $request['uploaded_' . $parameter];
|
||||
|
||||
if (count($medias) == count($uploaded)) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($uploaded as $old_media) {
|
||||
$already_uploaded[] = $old_media['id'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ((array)$medias as $media) {
|
||||
if (in_array($media->id, $already_uploaded)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
MediaModel::where('id', $media->id)->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user