Search code examples
phplaravelroutesdelete-filepublic-method

cant delete image from storage laravel


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');
    }
    
}

Solution

  • 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.