Skip to content

Commit e669222

Browse files
authored
Introduce new method updateInto
Introduce new method `updateInto`. It will help us to upload new file, delete old file and save at a time.
1 parent 1d29746 commit e669222

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

src/HasUploader.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,31 @@ public function saveInto($column = null, $saveAsArray = false)
148148
return false;
149149
}
150150

151+
/**
152+
* Save the particular file path into the model property and delete the old resource
153+
*
154+
* @param string $column
155+
* @return bool
156+
*
157+
*/
158+
public function updateInto($column = null)
159+
{
160+
$column_name = $column ?? $this->request_input_field;
161+
162+
// remove old res
163+
164+
$file = public_path($this->$column_name);
165+
if (is_file($file)) {
166+
unlink($file);
167+
}
168+
169+
if (count($this->uploaded_files)) {
170+
$this->$column_name = $this->uploaded_files[0];
171+
return $this->save();
172+
}
173+
return false;
174+
}
175+
151176

152177
/**
153178
* Get all the uploaded files path as array
@@ -170,9 +195,7 @@ public function getUploadedFiles()
170195
public function deleteWith($column): bool
171196
{
172197
$file = public_path($this->$column);
173-
if (is_file($file)) {
174-
unlink($file);
175-
}
198+
if (is_file($file)) unlink($file);
176199
return $this->delete();
177200
}
178201
}

0 commit comments

Comments
 (0)