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!
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();