Skip to content

Commit 1ab978a

Browse files
committed
fix custom service
1 parent 3333ae7 commit 1ab978a

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

app/Http/Controllers/Backend/Service/CustomServiceController.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,11 @@ public function update(CustomServiceRequest $request, CustomService $customServi
7070
{
7171
if ($request->has('image')) {
7272
$image = $request->file('image');
73-
$customService->updateImage($image, $customService->image->path);
73+
if ($customService->image) {
74+
$customService->updateImage($image, $customService->image?->path);
75+
}else{
76+
$customService->saveImage($image);
77+
}
7478
}
7579
$service = $this->service->update(CustomServiceDto::transformRequest($request), $customService);
7680
return redirect(route('custom-service.index'))->with([
@@ -85,7 +89,7 @@ public function update(CustomServiceRequest $request, CustomService $customServi
8589
public function destroy(CustomService $customService)
8690
{
8791
if ($customService->image) {
88-
$customService->deleteImage($customService->image->path);
92+
$customService->deleteImage($customService->image?->path);
8993
}
9094
$this->service->delete($customService);
9195
return redirect(route('custom-service.index'))->with([

app/Http/Requests/CustomService/CustomServiceRequest.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace App\Http\Requests\CustomService;
44

55
use App\Enums\PageName;
6-
use Illuminate\Validation\Rule;
76
use Illuminate\Foundation\Http\FormRequest;
7+
use Illuminate\Validation\Rule;
88

99
class CustomServiceRequest extends FormRequest
1010
{
@@ -17,11 +17,11 @@ class CustomServiceRequest extends FormRequest
1717
public function rules(): array
1818
{
1919
return [
20-
'page_name'=> ['required',Rule::enum(PageName::class)],
21-
'title'=>['required','string','max:255'],
22-
'description'=> ['required', 'string'],
23-
'link'=> ['required', 'starts_with:https://,http://'],
24-
'image'=> ['nullable','image', 'max:5124']
25-
];
20+
'page_name' => [ 'required', Rule::enum(PageName::class) ],
21+
'title' => [ 'required', 'string', 'max:255' ],
22+
'description' => [ 'required', 'string' ],
23+
'link' => [ 'required', 'starts_with:https://,http://' ],
24+
'image' => [ 'sometimes', 'required', 'image', 'max:5124' ],
25+
];
2626
}
2727
}

app/Traits/HasImage.php

+24-26
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@
22

33
namespace App\Traits;
44

5-
use Carbon\Carbon;
6-
use App\Models\Image;
7-
use Illuminate\Support\Str;
85
use App\DTOs\Image\ImageDto;
6+
use App\Models\Image;
7+
use Carbon\Carbon;
98
use Illuminate\Database\Eloquent\Relations\MorphMany;
9+
use Illuminate\Database\Eloquent\Relations\MorphOne;
1010
use Illuminate\Http\UploadedFile;
1111
use Illuminate\Support\Facades\Storage;
12-
use Illuminate\Database\Eloquent\Relations\MorphOne;
12+
use Illuminate\Support\Str;
1313

1414
trait HasImage
1515
{
16-
protected string $disk = 'public';
16+
protected string $disk = 'public';
1717
protected string $imageableType = __CLASS__;
1818

1919
public function image(): MorphOne
@@ -26,14 +26,12 @@ public function images(): MorphMany
2626
return $this->morphMany(Image::class, 'imageable');
2727
}
2828

29-
30-
3129
public function saveImage(UploadedFile $image, string $dir = null, ?string $prefix = null, ?string $disk = null)
3230
{
33-
$dir = $dir ?? $this->getBaseDir();
34-
$prefix = $prefix ?? $this->getPrefix();
35-
$disk = $disk ?? $this->disk;
36-
$path = $this->storeOnDisk($image, $dir, $prefix, $disk);
31+
$dir = $dir ?? $this->getBaseDir();
32+
$prefix = $prefix ?? $this->getPrefix();
33+
$disk = $disk ?? $this->disk;
34+
$path = $this->storeOnDisk($image, $dir, $prefix, $disk);
3735
$imageDto = new ImageDto(
3836
imageable_id: $this->id,
3937
imageable_type: $this->imageableType,
@@ -45,22 +43,23 @@ public function saveImage(UploadedFile $image, string $dir = null, ?string $pref
4543
return $image;
4644
}
4745

48-
public function updateImage(UploadedFile $image, string $path, string $dir = null, ?string $prefix = null, ?string $disk = null)
46+
public function updateImage(UploadedFile $image, string|null $path, string $dir = null, ?string $prefix = null, ?string $disk = null)
4947
{
50-
$dir = $dir ?? $this->getBaseDir();
48+
$dir = $dir ?? $this->getBaseDir();
5149
$prefix = $prefix ?? $this->getPrefix();
52-
$disk = $disk ?? $this->disk;
53-
$this->deleteImageFromDisk($path, $disk);
54-
$path = $this->storeOnDisk($image, $dir, $prefix, $disk);
55-
$image = $this->image()->update([
56-
'path' => $path,
57-
'url' => asset('storage/' . $path),
58-
'mime_type' => $image->extension()
59-
]);
60-
return $image;
50+
$disk = $disk ?? $this->disk;
51+
if ($this->image) {
52+
$this->deleteImageFromDisk($path, $disk);
53+
$path = $this->storeOnDisk($image, $dir, $prefix, $disk);
54+
$image = $this->image()->update([
55+
'path' => $path,
56+
'url' => asset('storage/' . $path),
57+
'mime_type' => $image->extension(),
58+
]);
59+
}
60+
return true;
6161
}
6262

63-
6463
public function deleteImageFromDisk(string $path, ?string $disk = null)
6564
{
6665
$disk = $disk ?? $this->disk;
@@ -75,7 +74,6 @@ public function deleteImage(string $path, ?string $disk = null): void
7574
$this->image()->delete();
7675
}
7776

78-
7977
protected function getTimestamp(): string
8078
{
8179
return Carbon::now()->format('Y-m-d-H-m-s-u');
@@ -88,7 +86,7 @@ protected function storeOnDisk(UploadedFile $file, string $dir, string $prefix,
8886

8987
protected function getName(UploadedFile $file): string
9088
{
91-
$ext = "." . $file->extension();
89+
$ext = "." . $file->extension();
9290
$name = $this->getPrefix() . "-" . $this->getTimestamp() . $ext;
9391
return $name;
9492
}
@@ -101,7 +99,7 @@ protected function storeOnDatabase(ImageDto $dto): Image
10199
'path' => $dto->path,
102100
'url' => $dto->url,
103101
'mime_type' => $dto->mime_type,
104-
]);
102+
]);
105103
}
106104

107105
/**

resources/views/backend/service/custom/edit.blade.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
<x-backend.ui.section-card name="Edit Custom Service">
66

7-
<form action="{{ route('custom-service.store') }}" method="post" enctype="multipart/form-data">
7+
<form action="{{ route('custom-service.update', $service) }}" method="post" enctype="multipart/form-data">
88
@csrf
9+
@method('PUT')
910
<div class="row">
1011
<div class="col-md-6 mt-3">
11-
<x-backend.form.image-input :image="$service->image->url" name="image" />
12+
<x-backend.form.image-input :image="$service?->image?->url" name="image" />
1213
</div>
1314

1415
<div class="col-md-6">
@@ -21,7 +22,7 @@
2122
<x-backend.form.select-input id="page_name" label="Page Name" name="page_name"
2223
placeholder="Choose page name...">
2324
@foreach ($pageNames as $name)
24-
<option value="{{ $name }}" @selected(str($service->page_name)->trim() === str($name->value))>
25+
<option value="{{ $name }}" @selected($service->page_name == $name)>
2526
{{ $name }}
2627
</option>
2728
@endforeach

resources/views/components/backend/form/select-input.blade.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
</label>
1616
@endif
1717
<select {{ $attributes->merge(['class' => 'form-select text-capitalize px-3 py-2'])->merge() }}>
18-
<option selected disabled>{{ $placeholder }}</option>
1918
{{ $slot }}
2019
</select>
2120
@error($name)

0 commit comments

Comments
 (0)