Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add update_database option #50

Merged
merged 2 commits into from
Feb 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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