Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FinleyCox's avatar

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?

0 likes
27 replies
skeith22's avatar

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?

Sinnbeck's avatar

Your path seems off. Where is the image saved exactly? /storage/?

skeith22's avatar

you should be using File Storage here is for reference, https://laravel.com/docs/9.x/filesystem

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

your images/photos should be stored on storage/app/public

for example

  • storage/app/public/stores/your_image_1.jpg
  • storage/app/public/stores/your_image_2.jpg

When checking if it exists

use Illuminate\Support\Facades\Storage;

if (Storage::exists('your_image_name.jpg')) {
    // ...
}

When checking if it's missing

use Illuminate\Support\Facades\Storage;

if (Storage::missing('file.jpg')) {
    // ...
}

When saving

use Illuminate\Http\File;
use Illuminate\Support\Facades\Storage;
 
if (! $path = Storage::putFile('shops', new File($request->file('image')))) {
    // The file could not be written to disk... so you could check for an error
}

When retrieving

use Illuminate\Support\Facades\Storage;

$path = Storage::path('your_image_name.jpg');

When deleting

use Illuminate\Support\Facades\Storage;
 
if (!Storage::delete('your_image_name.jpg')) {
    // something went wrong when deleting the file
}

Getting the image url

use Illuminate\Support\Facades\Storage;

$url = Storage::url('your_image_file.jpg');
skeith22's avatar

@FinleyCox let me know how it goes, just combine these codes on what you want to happen, you'll be fine! :D

skeith22's avatar

@Sinnbeck he would have to create a public disk for that path, so it could be display publicly though. I suggest he would put his images on the storage/app/public folder.

but since by default the Storage class uses the public folder, so he should be fine with this samples.

skeith22's avatar

@Sinnbeck it's safe to assume it doesn't work though, cause that would've been private, kinda curious if it's really displaying before

Sinnbeck's avatar

@skeith22 I have seen people use routes for displaying private images before so its possible. But don't know if it's the case here :)

skeith22's avatar

@Sinnbeck that's really crazy, I'm actually amused as to what people could think of making things works, but maybe Laravel has prevented that to work now? since that's a security risk, files are privately stored for a reason, if they want it public, should've put in on a public folder.

I'm now kind of curious if this still works? just my curiosity bugging me lol

Sinnbeck's avatar

@skeith22 yeah you can do that if you want. Laravel supports sending the header and content if any file you ask it to :)

In theory it can be used to make sure only the owner of the images can see them

Sinnbeck's avatar

And what exactly happens? An error? Or no overwrite? Are you sure the form has the correct enctype?

FinleyCox's avatar

@Sinnbeck thank you for that code, what exactly happens is no overwrite. Now I tried your code and can have got a new image but no changed. (used dd) And yes, I'm sure that form is correct.

skeith22's avatar

@FinleyCox if your request has the image, meaning you just have to delete the image and add a new one. you can check the sample codes I commented for checking, storing, retrieving, and deleting files.

just play around with your logic to make it work! :D

skeith22's avatar
skeith22
Best Answer
Level 21

@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');
FinleyCox's avatar

@skeith22 Thank you for that!! I am trying now :D Actually, I've already done that(link)

Sinnbeck's avatar

@FinleyCox then use the storage facade as suggested with the public disk. Its what it's for

skeith22's avatar

@FinleyCox make sure your files are being stored in storage/app/public. I'm assuming it should look like storage/app/public/stores/your_image_name.jpg.

FinleyCox's avatar

@skeith22 Thank you so much. It helped me a lot and it was successfully changed pruoblemlessly

Please or to participate in this conversation.