在Laravel 5.5中删除文件
问题描述:
我正在使用Laravel 5.5,并试图删除存储在我的storage/app/public
目录中的文件。 我试图从数据库中检索文件路径并删除它,但它不工作。在Laravel 5.5中删除文件
$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 pluck返回一个雄辩的集合,而不是一个数组。 尝试使用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);
你在Windows或Linux – john