From 4d5fa0eb1de1011391161f118e9ad6afe708b960 Mon Sep 17 00:00:00 2001 From: MALLEN Date: Tue, 12 May 2020 01:23:05 +0200 Subject: [PATCH 1/2] add update_database option not update database field (eg: if don't exist), but do all others things --- README.md | 3 +++ src/HasImageUploads.php | 19 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 0d9f2f0..695a6e6 100644 --- a/README.md +++ b/README.md @@ -127,6 +127,9 @@ class User extends Model { // if request file is don't have same name, default will be the field name 'file_input' => 'photo', + // if field (here "avatar") don't exist in database or you wan't this field in database + 'update_database' => false, + // a hook that is triggered before the image is saved 'before_save' => BlurFilter::class, diff --git a/src/HasImageUploads.php b/src/HasImageUploads.php index 5301e7d..122a89c 100644 --- a/src/HasImageUploads.php +++ b/src/HasImageUploads.php @@ -569,12 +569,19 @@ protected function saveImage($imageFile, $image) */ protected function updateModel($imagePath, $imageFieldName) { - $this->attributes[$imageFieldName] = $imagePath; - - $dispatcher = $this->getEventDispatcher(); - self::unsetEventDispatcher(); - $this->save(); - self::setEventDispatcher($dispatcher); + // check if update_database = false (default: true) + $imagesFields = $this->getDefinedUploadFields(); + $actualField=Arr::get($imagesFields, $imageFieldName); + $updateAuthorized=Arr::get($actualField, 'update_database',true); + + // update model (if update_database=true or not set) + if($updateAuthorized){ + $this->attributes[$imageFieldName] = $imagePath; + $dispatcher = $this->getEventDispatcher(); + self::unsetEventDispatcher(); + $this->save(); + self::setEventDispatcher($dispatcher); + } } /** From 87188ffe083e9e6ada1eec29ab81cadd6fb35b48 Mon Sep 17 00:00:00 2001 From: MALLEN Date: Tue, 12 May 2020 02:14:36 +0200 Subject: [PATCH 2/2] Correction ImageTag --- src/HasImageUploads.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/HasImageUploads.php b/src/HasImageUploads.php index 122a89c..dcea515 100644 --- a/src/HasImageUploads.php +++ b/src/HasImageUploads.php @@ -85,7 +85,11 @@ public function imageUrl($field = null) $this->uploadFieldOptions = $this->getUploadFieldOptions($this->uploadFieldName); // get the model attribute value - $attributeValue = $this->getOriginal($this->uploadFieldName); + if(Arr::get($this->uploadFieldOptions, 'update_database',true)){ + $attributeValue = $this->getOriginal($this->uploadFieldName); + }else{ + $attributeValue = $this->getFileUploadPath($field); + } // check for placeholder defined in option $placeholderImage = Arr::get($this->uploadFieldOptions, 'placeholder');