Last change
on this file since 3536fe9 was b9c4a92, checked in by Berat Kjufliju <kufliju@…>, 3 years ago |
edited file upload, seeding and departments controller
|
-
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 | {
|
---|
[b9c4a92] | 13 | dd($object);
|
---|
[24a616f] | 14 | $objIds = [];
|
---|
| 15 |
|
---|
| 16 | foreach ($names as $name => $item) {
|
---|
| 17 |
|
---|
| 18 | $property = $name;
|
---|
| 19 |
|
---|
| 20 | if ($fieldName) $property = $fieldName;
|
---|
| 21 |
|
---|
| 22 | if (!request()->hasFile($name)) {
|
---|
| 23 | continue;
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 | if (!$isMultiple) {
|
---|
| 27 | if (!is_null($object->$property)) {
|
---|
| 28 | Storage::disk("image-uploads")->delete($object->$property);
|
---|
| 29 | }
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 | $images = request()->file($name);
|
---|
| 33 |
|
---|
| 34 | if(!is_array($images)) $images = [$images];
|
---|
| 35 |
|
---|
| 36 | foreach ($images as $image) {
|
---|
| 37 |
|
---|
| 38 | if($isMultiple) {
|
---|
| 39 | $object = new $object;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | $extension = $image->getClientOriginalExtension();
|
---|
| 43 | $fileName = Str::random(20) . time() . "." . $extension;
|
---|
| 44 |
|
---|
| 45 |
|
---|
| 46 | Storage::disk("image-uploads")->put(
|
---|
| 47 | $fileName,
|
---|
| 48 | File::get($image)
|
---|
| 49 | );
|
---|
| 50 |
|
---|
| 51 | $object->$property = $fileName;
|
---|
| 52 |
|
---|
| 53 | if ($fieldName) $object->save();
|
---|
| 54 | array_push($objIds, $object->id);
|
---|
| 55 | }
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | return $objIds;
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
Note:
See
TracBrowser
for help on using the repository browser.