【发布时间】:2019-05-15 00:33:04
【问题描述】:
我在让 Storage::delete($filepath); 在 Laravel 5.4 中工作时遇到问题。
我已经搜索过其他有此问题的人,但大多数其他人似乎遇到的错误是提供的文件路径没有前面的/,但这不是我的问题。
我正在使用use Illuminate\Support\Facades\Storage;(根据Laravel Docs),我注意到我在PHPStorm 中收到错误消息Method delete not found in Illuminate\Support\Facades\Storage。
我的代码是这样的;
<?php
namespace App\Http\Controllers;
...
use Illuminate\Support\Facades\Storage;
// also tried use Storage;
...
public function deleteFile($id)
{
try {
$image = Files::where('id', $id)->get()->first();
Storage::delete($image->filepath);
return Files::destroy($id);
} catch ( \Exception $e) {
return back()->with('alert-warning', 'Something went wrong: ' . $e);
}
}
我的 $image->文件路径看起来像 /Users/usrname/sites/sitename/storage/app/images/34/o8Aq1T3Hi67sOtuTgBh9P7QWA1Ahj4KH2QBR77n0.png
有人可以帮忙吗?
【问题讨论】:
-
它不应该在立面上。看看外观是如何工作的:laravel.com/docs/5.4/facades#how-facades-work 另外,github.com/barryvdh/laravel-ide-helper 是一个很棒的开发工具,有助于防止这些 IDE 问题并提供更好的代码完成
-
@Pevara 谢谢你,我会去看看。你有什么想法为什么这不起作用?不幸的是,我没有看到任何错误消息..?
-
应该是
use Storage,而不是use Illuminate\Support\Facades\Storage -
(
Files::findOrFail($id)也可能更干净,但这与您的问题无关) -
我尝试使用存储,不幸的是没有解决它。