【问题标题】:Deleting file in Laravel 5.5在 Laravel 5.5 中删除文件
【发布时间】:2017-09-26 17:29:16
【问题描述】:

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

但它起作用了...... 我真的很困惑为什么我不能使用这个变量!

【问题讨论】:

  • 你是windows还是linux

标签: php laravel delete-file laravel-5.5


【解决方案1】:

Laravel pluck 返回一个雄辩的集合而不是一个数组。 尝试使用 all()toArray() 函数将集合转换为数组

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

或者

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

【讨论】:

    【解决方案2】:

    答案是:

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

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 1970-01-01
      • 2018-04-20
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-10
      • 2020-09-10
      相关资源
      最近更新 更多