How do you delete a single Media item? #1908
-
How do I delete a single media item from it's ID? Right now I have
Which deletes the DB record, but it does NOT delete the files. Any suggestions would be helpful, I looked all through the docs and didn't see anywhere it referenced deleting of single item. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You can add a listener for the delete event of that model |
Beta Was this translation helpful? Give feedback.
-
EDIT: My guess is that, in order for this to work, you need to call Does this work? Media::find($request->get('document_id'))->delete(); Original reply: Media::find($request->input('photo'))->delete(); to delete a media from its ID. Otherwise, I see that the /**
* Delete the associated media with the given id.
* You may also pass a media object.
*
* @param int|\Spatie\MediaLibrary\MediaCollections\Models\Media $mediaId
*
* @throws \Spatie\MediaLibrary\MediaCollections\Exceptions\MediaCannotBeDeleted
*/
public function deleteMedia($mediaId): void
{
// ...
}
public function deletePreservingMedia(): bool
{
// ...
} |
Beta Was this translation helpful? Give feedback.
EDIT:
The package uses an observer on the Eloquent model. This is where the deletion of files take place.
My guess is that, in order for this to work, you need to call
delete()
directly on an instance of your media model, and not on the query builder as you do it in your example. I think you need a real Eloquent instance to call thedelete()
method on in order for the observer to be triggered.Does this work?
Original reply:
In my controller, I call
to delete a media from its ID.
Otherwise, I see that the
InteractsWithMedia
trait contains these two methods: