0

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!

Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
Ali Jouadi
  • 133
  • 1
  • 2
  • 15

2 Answers2

1

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();
deepanshu223
  • 433
  • 2
  • 6
1

The answer is:

$file = Blog::where('id', $id)->first()->image;
Storage::delete($file);
Kornél Takó
  • 321
  • 3
  • 7