Search code examples
phplaraveldelete-filelaravel-5.5

Deleting file in Laravel 5.5


I'm using Laravel 5.5 and I'm trying to delete a file stored in my storage/app/public directory. I'm trying to retrieve the file path from database and delete it, but it's not working.

$file = Blog::where('id', $id)->pluck('image');

Storage::delete($file);

I'm retrieving the file's path successfully:

echo $file = Blog::where('id', $id)->pluck('image');

because the above line gets me this:

["public\/blog\/tLL0JoDmAKadTL0SqFZp1Is4oAK3YUo0GjOWB3fh.jpeg"]

so I thought something is wrong with using the storage facade and the delete method, and I tried this:

Storage::delete(["public\/blog\/tLL0JoDmAKadTL0SqFZp1Is4oAK3YUo0GjOWB3fh.jpeg"]);

but it worked... and I'm really confused why I can't use the variable!


Solution

  • Laravel pluck returns an eloquent collection and not an array. Try converting the collection to an array using the all() or toArray() functions

    $file = Blog::where('id', $id)->pluck('image')->all();
    

    Or

    $file = Blog::where('id', $id)->pluck('image')->toArray();