Skip to content

Commit 1fd2369

Browse files
committed
Save the full URL of the image in DB
1 parent 614d370 commit 1fd2369

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

app/Http/Controllers/Api/ProductController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Illuminate\Http\Request;
1111
use Illuminate\Http\UploadedFile;
1212
use Illuminate\Support\Facades\Storage;
13+
use Illuminate\Support\Facades\URL;
1314
use Illuminate\Support\Str;
1415

1516
class ProductController extends Controller
@@ -51,7 +52,7 @@ public function store(ProductRequest $request)
5152
// Check if image was given and save on local file system
5253
if ($image) {
5354
$relativePath = $this->saveImage($image);
54-
$data['image'] = $relativePath;
55+
$data['image'] = URL::to(Storage::url($relativePath));
5556
$data['image_mime'] = $image->getClientMimeType();
5657
$data['image_size'] = $image->getSize();
5758
}
@@ -89,7 +90,7 @@ public function update(ProductRequest $request, Product $product)
8990
// Check if image was given and save on local file system
9091
if ($image) {
9192
$relativePath = $this->saveImage($image);
92-
$data['image'] = $relativePath;
93+
$data['image'] = URL::to(Storage::url($relativePath));
9394
$data['image_mime'] = $image->getClientMimeType();
9495
$data['image_size'] = $image->getSize();
9596

app/Http/Resources/ProductListResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function toArray($request)
1919
return [
2020
'id' => $this->id,
2121
'title' => $this->title,
22-
'image_url' => $this->image ? Url::to(Storage::url($this->image)) : null,
22+
'image_url' => $this->image ?: null,
2323
'price' => $this->price,
2424
'updated_at' => (new \DateTime($this->updated_at))->format('Y-m-d H:i:s'),
2525
];

app/Http/Resources/ProductResource.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function toArray($request)
2323
'title' => $this->title,
2424
'slug' => $this->slug,
2525
'description' => $this->description,
26-
'image_url' => $this->image ? Url::to(Storage::url($this->image)) : null,
26+
'image_url' => $this->image ?: null,
2727
'price' => $this->price,
2828
'created_at' => (new \DateTime($this->created_at))->format('Y-m-d H:i:s'),
2929
'updated_at' => (new \DateTime($this->updated_at))->format('Y-m-d H:i:s'),

0 commit comments

Comments
 (0)