Skip to content

Commit

Permalink
Merge pull request #50 from tomredhot/master
Browse files Browse the repository at this point in the history
add update_database option
  • Loading branch information
saqueib authored Feb 8, 2022
2 parents 17f577a + 87188ff commit 171b166
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand Down
25 changes: 18 additions & 7 deletions src/HasImageUploads.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -569,12 +573,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);
}
}

/**
Expand Down

0 comments on commit 171b166

Please sign in to comment.