2017-09-25 156 views
0

我正在使用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"]); 

但它的工作... 我真的很困惑,爲什麼我不能使用變量!

+0

你在Windows或Linux – john

回答

0

Laravel pluck返回一個雄辯的集合,而不是一個數組。 嘗試使用all()toArray()功能

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

或者

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

答案是集合轉換爲數組:

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