I am having an issue getting Storage::delete($filepath);
to work in 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
.
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
Anyone able to help?
One way to delete a file from the public directory in Laravel is to use the Storage facade. To delete a file, you will need to follow the following steps: Step 1: Check to ensure that the folder and file exist. Step 2: Delete the required file.
To delete a directory in the Laravel application you can make use of the "deleteDirectory()" method available from the Storage facade. <? php Storage::deleteDirectory($directory); This method will delete the directory with all of its files so you must carefully ensure that you are deleting the right directory.
The deleteDirectory() is a method that accepts the path of the directory and deletes it. The public_path() is a method Laravel uses to fetch the folder passed as a parameter to the path of the directory.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With