正在删除 Laravel 5.5 中的文件

Deleting file in Laravel 5.5

我正在使用 Laravel 5.5 并试图删除存储在 storage/app/public 目录中的文件。 我正在尝试从数据库中检索文件路径并将其删除,但它不起作用。

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

Storage::delete($file);

我正在成功检索文件的路径:

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

因为上面这行让我明白了:

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

所以我认为使用存储门面和删除方法有问题,我尝试了这个:

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

但它奏效了... 我真的很困惑为什么我不能使用变量!

Laravel 提取 returns 一个 eloquent 集合而不是数组。 尝试使用 all()toArray() 函数将集合转换为数组

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

或者

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

答案是:

$file = Blog::where('id', $id)->first()->image;
Storage::delete($file);