Reputation: 2513
Using Laravel Mediable and I'm trying to figure out the best way to delete an individual file. Say there's a list of files when I'm viewing the parent Model, and I click a delete icon to make an ajax request to delete file (which should remove both the corresponding Media object and the physical file...)
This works:
$path_parts = pathinfo($request->filename);
$attachment = Media::where('directory', $folder)
->where('filename', $path_parts['filename'])
->where('extension', $path_parts['extension'])
->first();
$attachment->delete();
but this deletes only the database row and not the physical file itself:
$attachment = Media::where('id', $request->fileid);
$attachment->delete();
I'd prefer deleting the file via the id because its unique but wondering what I'm missing...
Upvotes: 0
Views: 306
Reputation: 11
You must delete the file also using this code
File::delete('path/to/'.$request->filename);
=updated= The problem on the second block is just need to add ->first() at the end of the where clause
Upvotes: 1