Hello, so i was building an e-commerce website and i was in the middle of building the CRUD features for managing products. I got the problem when i try to view the products, while it's showing another attributes like the product name, price, weight, etc, the image was broken.
Here is my Controller:
public function store(Request $request)
{
$this->validate($request, [
'name' => 'required|string|max:100',
'description' => 'required',
'category_id' => 'required|exists:categories,id',
'price' => 'required|integer',
'weight' => 'required|integer',
'image' => 'required|image|mimes:png,jpeg,jpg'
]);
if ($request->hasFile('image')) {
$file = $request->file('image');
$filename = time() . Str::slug($request->name) . '.' . $file->getClientOriginalExtension();
$file->storeAs('public/products', $filename);
$product = Product::create([
'name' => $request->name,
'slug' => $request->name,
'category_id' => $request->category_id,
'description' => $request->description,
'image' => $filename,
'price' => $request->price,
'weight' => $request->weight,
'status' => $request->status
]);
return redirect(route('product.index'))->with(['success' => 'Products Added']);
}
}
And this is where i want to view the images:
forelse ($product as $row)
<tr>
<td>
<img src="{{ asset('storage/products/' . $row->image) }}" width="100px" height="100px" alt="{{ $row->name }}">
</td>
<td>
<strong>{{ $row->name }}</strong><br>
<label>Category: <span class="badge badge-info">{{ $row->category->name }}</span></label><br>
<label>Weight: <span class="badge badge-info">{{ $row->weight }} gr</span></label>
</td>
<td>$ {{ number_format($row->price) }}</td>
<td>{{ $row->created_at->format('d-m-Y') }}</td>
<td>{!! $row->status_label !!}</td>
<td>
<form action="{{ route('product.destroy', $row->id) }}" method="post">
@csrf
@method('DELETE')
<a href="{{ route('product.edit', $row->id) }}" class="btn btn-warning btn sm">Edit</a>
<button class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
@empty
<tr>
<td colspan="5" class="text-center">No Data</td>
</tr>
@endforelse
Before it was showing, but when i try to change to another template for my website, not long after this it suddenly broken. And when i checked the file in storage/app/public/products/ directory the files is in there and it can be deleted or even updated from the administrator panel but i still can't figure out what causing it.
Is there any suggestion?
Thank you.