Skip to content

Commit b2cdeb3

Browse files
committed
add 2 new methods
1 parent dbcbf0e commit b2cdeb3

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"email": "[email protected]"
2222
}
2323
],
24+
"require": {},
2425
"minimum-stability": "dev",
25-
"require": {}
26+
"prefer-stable": true
2627
}

composer.lock

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HasUploader.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ trait HasUploader
2121
public $uploaded_files = [];
2222

2323

24+
/**
25+
* name of the model.
26+
*
27+
* @var array
28+
*
29+
*/
30+
public $model_name;
31+
32+
2433
/**
2534
* Request file input file field name
2635
*
@@ -120,11 +129,14 @@ public function uploadRequestFile($request_input_field)
120129
*/
121130
public function upload(UploadedFile $file, $module_name = null, $file_type = 'images', $unique_id = null)
122131
{
123-
$module_name = $module_name ?? $this->getTable();
132+
$model_name = $model_name ?? $this->getTable();
133+
134+
$this->model_name = $model_name;
135+
124136
$unique_id = $unique_id ?? uniqid();
125137

126-
$file_name = "{$module_name}-{$unique_id}.{$file->extension()}";
127-
$dir = "uploads/{$module_name}/{$file_type}/";
138+
$file_name = "{$model_name}-{$unique_id}.{$file->extension()}";
139+
$dir = "uploads/{$model_name}/{$file_type}/";
128140
$file_path = $dir . $file_name;
129141
$file->move(public_path($dir), $file_name);
130142
array_push($this->uploaded_files, $file_path);
@@ -185,17 +197,45 @@ public function getUploadedFiles()
185197
return $this->uploaded_files;
186198
}
187199

200+
/**
201+
* Remove the specified resource from storage according of the model.
202+
*
203+
* @param string $column
204+
* @return \Plusemon\Uploader\HasUploader
205+
*
206+
*/
207+
public function deleteFile($column)
208+
{
209+
$file = public_path($this->$column);
210+
if (is_file($file)) return unlink($file);
211+
return false;
212+
}
213+
188214
/**
189215
* Remove the specified resource from storage according to the model.
190216
*
191217
* @param string $column
192218
* @return bool
193219
*
194220
*/
195-
public function deleteWith($column): bool
221+
public function deleteWithFile($column): bool
196222
{
197-
$file = public_path($this->$column);
198-
if (is_file($file)) unlink($file);
223+
$this->deleteFile($column);
199224
return $this->delete();
200225
}
226+
227+
228+
// alieses
229+
230+
/**
231+
* Remove the specified resource from storage according to the model.
232+
*
233+
* @param string $column
234+
* @return bool
235+
*
236+
*/
237+
public function deleteWith($column): bool
238+
{
239+
return $this->deleteWithFile($column);
240+
}
201241
}

0 commit comments

Comments
 (0)