1

hello everybody im trying to delete an image from local storage with this function but it returns

Call to a member function delete() on string

which is right because when I use dd(destination); this is the result:

public/imagen/1634302328.jpg     

is there a way to convert that string into a route path or other solution to delete an image from storage? thanks

this is my function:

public function destroy($id)
    {   
 
        $autoridad = Autoridad::find($id);
        $destination = 'public/imagen/'.$autoridad->imagen;
        if(File::exists($destination)){
            File::delete($destination);
        }
        $destination->delete();
        return redirect()->route('autoridades.index');
    }
    
}
John Lobo
  • 14,355
  • 2
  • 10
  • 20
Martin2236
  • 33
  • 5

1 Answers1

1

I think you meant $autoridad->delete() instead of $destination->delete(). You assigned $destination to the string location of the image. That is why it does not allow you to call delete() on it.

Eelke van den Bos
  • 1,423
  • 1
  • 13
  • 18
  • if i do that, the data is deleted from de data base but the image still remains in the storage. i think the problem in that laravel can´t convert the string wich contains the path to the image ("public/imagen/1634302328.jpg ") – Martin2236 Oct 15 '21 at 14:22
  • Be sure to check whether the file actually can be found. You could add an `else { echo "File not found " . $destination; } – Eelke van den Bos Oct 15 '21 at 14:41
  • thanks you where right, laravel can not find the file. now i must look more information why is this happening – Martin2236 Oct 15 '21 at 14:59
  • You could try to use the `public_path` helper to find the fully qualified path (absolute) to the file you are referring to. (docs https://laravel.com/docs/8.x/helpers#method-public-path) – Eelke van den Bos Oct 15 '21 at 15:18
  • Thank you very much!!! it worked, using a public path and unlink() instead delete(); – Martin2236 Oct 15 '21 at 15:37