Laravel 5.4 Storage::delete() 不起作用(找不到方法删除)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43922363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:56:58  来源:igfitidea点击:

Laravel 5.4 Storage::delete() not working (method delete not found)

phplaravellaravel-5

提问by Ash

I am having an issue getting Storage::delete($filepath);to work in Laravel 5.4.

Storage::delete($filepath);在 Laravel 5.4 中工作时遇到问题。

I have searched for other people with this issue, but the error most others seem to have is providing the file path without the preceding /, however this is not my issue.

我已经搜索了其他有此问题的人,但大多数其他人似乎都有的错误是提供没有前面的文件路径/,但这不是我的问题。

I am using use Illuminate\Support\Facades\Storage;(as per the Laravel Docs) and I have noticed I am getting an error in PHPStorm saying Method delete not found in Illuminate\Support\Facades\Storage.

我正在使用use Illuminate\Support\Facades\Storage;(根据Laravel 文档)并且我注意到我在 PHPStorm 中收到一个错误说Method delete not found in Illuminate\Support\Facades\Storage

My code looks like;

我的代码看起来像;

<?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);
    }
}

My $image->filepath looks like /Users/usrname/sites/sitename/storage/app/images/34/o8Aq1T3Hi67sOtuTgBh9P7QWA1Ahj4KH2QBR77n0.png

我的 $image->filepath 看起来像 /Users/usrname/sites/sitename/storage/app/images/34/o8Aq1T3Hi67sOtuTgBh9P7QWA1Ahj4KH2QBR77n0.png

Anyone able to help?

任何人都可以提供帮助?

采纳答案by Ash

OK so it turns out that the $filepath needs to be relative to the storage root of the app, not the full file path to the file!

好的,事实证明 $filepath 需要相对于应用程序的存储根目录,而不是文件的完整文件路径!

I used a function to update my file path to;

我使用了一个函数来更新我的文件路径;

images/34/o8Aq1T3Hi67sOtuTgBh9P7QWA1Ahj4KH2QBR77n0.pngand it works a charm.

images/34/o8Aq1T3Hi67sOtuTgBh9P7QWA1Ahj4KH2QBR77n0.png它很有魅力。

回答by José Lozano Hernandez

I had another problem, I was calling

我有另一个问题,我正在打电话

Storage::delete($path);

without a disk, so I put this, and it work.

没有磁盘,所以我放了这个,它可以工作。

Storage::disk('public')->delete($path);

回答by user2521

I was having the same error, tried File::delete() then it worked.

我遇到了同样的错误,尝试了 File::delete() 然后它起作用了。

回答by Djamel KHIREDDINE

in : config/filesystems.php by default :

在: config/filesystems.php 默认情况下:

'local' => [
        'driver' => 'local',
        'root' => storage_path('app'),
    ],

so if your image is in storage/app/public/ folder, your storage image path should be:

因此,如果您的图像在 storage/app/public/ 文件夹中,则您的存储图像路径应为:

'public/image.jpg'

'公共/图像.jpg'

To delete this image from storage you should write :

要从存储中删除此图像,您应该编写:

Storage::delete('public/image.jpg)