develop
Last change
on this file since e6c1f87 was 24a616f, checked in by Berat Kjufliju <kufliju@…>, 3 years ago |
added documents crud, added last_seen_to_user, edited views
|
-
Property mode
set to
100644
|
File size:
1.4 KB
|
Rev | Line | |
---|
[24a616f] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Services;
|
---|
| 4 |
|
---|
| 5 | use Illuminate\Support\Facades\File;
|
---|
| 6 | use Illuminate\Support\Facades\Storage;
|
---|
| 7 | use Illuminate\Support\Str;
|
---|
| 8 |
|
---|
| 9 | class UploadService
|
---|
| 10 | {
|
---|
| 11 | public function upload($object, $names, $fieldName = null, $isMultiple = false)
|
---|
| 12 | {
|
---|
| 13 | $objIds = [];
|
---|
| 14 |
|
---|
| 15 | foreach ($names as $name => $item) {
|
---|
| 16 |
|
---|
| 17 | $property = $name;
|
---|
| 18 |
|
---|
| 19 | if ($fieldName) $property = $fieldName;
|
---|
| 20 |
|
---|
| 21 | if (!request()->hasFile($name)) {
|
---|
| 22 | continue;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | if (!$isMultiple) {
|
---|
| 26 | if (!is_null($object->$property)) {
|
---|
| 27 | Storage::disk("image-uploads")->delete($object->$property);
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | $images = request()->file($name);
|
---|
| 32 |
|
---|
| 33 | if(!is_array($images)) $images = [$images];
|
---|
| 34 |
|
---|
| 35 | foreach ($images as $image) {
|
---|
| 36 |
|
---|
| 37 | if($isMultiple) {
|
---|
| 38 | $object = new $object;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | $extension = $image->getClientOriginalExtension();
|
---|
| 42 | $fileName = Str::random(20) . time() . "." . $extension;
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | Storage::disk("image-uploads")->put(
|
---|
| 46 | $fileName,
|
---|
| 47 | File::get($image)
|
---|
| 48 | );
|
---|
| 49 |
|
---|
| 50 | $object->$property = $fileName;
|
---|
| 51 |
|
---|
| 52 | if ($fieldName) $object->save();
|
---|
| 53 | array_push($objIds, $object->id);
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | return $objIds;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.