Skip to content

Commit 171b166

Browse files
authored
Merge pull request #50 from tomredhot/master
add update_database option
2 parents 17f577a + 87188ff commit 171b166

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ class User extends Model {
127127
// if request file is don't have same name, default will be the field name
128128
'file_input' => 'photo',
129129

130+
// if field (here "avatar") don't exist in database or you wan't this field in database
131+
'update_database' => false,
132+
130133
// a hook that is triggered before the image is saved
131134
'before_save' => BlurFilter::class,
132135

src/HasImageUploads.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ public function imageUrl($field = null)
8585
$this->uploadFieldOptions = $this->getUploadFieldOptions($this->uploadFieldName);
8686

8787
// get the model attribute value
88-
$attributeValue = $this->getOriginal($this->uploadFieldName);
88+
if(Arr::get($this->uploadFieldOptions, 'update_database',true)){
89+
$attributeValue = $this->getOriginal($this->uploadFieldName);
90+
}else{
91+
$attributeValue = $this->getFileUploadPath($field);
92+
}
8993

9094
// check for placeholder defined in option
9195
$placeholderImage = Arr::get($this->uploadFieldOptions, 'placeholder');
@@ -569,12 +573,19 @@ protected function saveImage($imageFile, $image)
569573
*/
570574
protected function updateModel($imagePath, $imageFieldName)
571575
{
572-
$this->attributes[$imageFieldName] = $imagePath;
573-
574-
$dispatcher = $this->getEventDispatcher();
575-
self::unsetEventDispatcher();
576-
$this->save();
577-
self::setEventDispatcher($dispatcher);
576+
// check if update_database = false (default: true)
577+
$imagesFields = $this->getDefinedUploadFields();
578+
$actualField=Arr::get($imagesFields, $imageFieldName);
579+
$updateAuthorized=Arr::get($actualField, 'update_database',true);
580+
581+
// update model (if update_database=true or not set)
582+
if($updateAuthorized){
583+
$this->attributes[$imageFieldName] = $imagePath;
584+
$dispatcher = $this->getEventDispatcher();
585+
self::unsetEventDispatcher();
586+
$this->save();
587+
self::setEventDispatcher($dispatcher);
588+
}
578589
}
579590

580591
/**

0 commit comments

Comments
 (0)