I am working on a Laravel 5.8 API application and I want to pass an image attribute with the value as the full url of the image. So I have created a symbolic link from public/storage to storage/app/public by running the artisan command
php artisan storage:link
I am storing the image in my controller like this
if ($product = Product::create([
'name' => $request->name,
'category' => $request->category,
'status' => $request->status,
'price' => $request->price,
'interest' => $request->interest,
])) {
// store the product image
$file = $request->file('image');
$destinationPath = "public/images/products";
$filename = $product->name . '_' . $product->id . '.' . $file->extension();
Storage::putFileAs($destinationPath, $file, $filename);
ProductImage::create([
'product_id' => $product->id,
'name' => $filename
]);
}
This is what the ProductResource looks like
return [
'id' => $this->id,
'name' => $this->name,
'category' => $this->category,
'status' => $this->status,
'price' => $this->price,
'interest' => $this->interest,
'image' => 'http://localhost:8000/api/v1/public/images/products/' . $this->image->name,
];
Reason why I want to return the full image path is so that the React application consuming the api can simply pass the image path inside the <img src="">
Is this proper way to do it?