Hey, I'm confused as to what you want to happen here, if I understand this correctly you want to update/overwrite your previous/old image with the new a image correct?
How to update image on Laravel
Hi, I would like to create web page using Laravel though, now my code of updating image doesn't work.
details are below
$shop = Shop::find($id);
$shop->name = $request->input('name');
$shop->email = $request->input('email');
$shop->address = $request->input('address');
$shop->number = $request->input('number');
$shop->description = $request->input('description');
if($request->hasFile('image'))
{
$destination = 'storage/shops/' . $shop->image;
if(File::exists($destination))
{
File::delete($destination);
}
$file = $request->file('image');
$extension = $file->getClientOriginalExtension();
$filename = time(). '.' . $extension;
$file->move('storage/shops/', $filename);
$shop->image = $filename;
}
$shop->save();
return redirect()->route('shops');
blade file
@foreach($shops as $shop)
<img src="{{ asset('storage/shops/' . $shop->image) }}"width="880" height="650" class="card" alt="No image" width="880" height="650">
<div class="card-body">
<h1 class="flex-auto text-lg font-semibold text-slate-900 text-center">
<a href="{{ route('shops_show', $shop->id) }}"><h5 class="card-title">{{ $shop->name }}</h5></a>
</h1>
<div class="w-full flex-none text-sm font-medium text-slate-700 mt-2 text-center">
<p class="card-text">{{ $shop->description }}</p>
Now : I can get request values except new image and can display previous image. I'm writing this to delete previous one and put new image in storage/shops/ .
could someone give me advice to solve this?
@FinleyCox was the old one displaying the image properly? if so just use this to display the image
make sure you have run this command php artisan storage:link, this will make your files available publicly, if you have, you can ignore this sentence. https://laravel.com/docs/9.x/filesystem#the-public-disk
also, make sure your images are stored in storage/app/public or it won't be displayed.
use Illuminate\Support\Facades\Storage;
$url = Storage::url('your_image_file.jpg');
Please or to participate in this conversation.