KikoLdasd's avatar

Soft delete spatie media library

Is there a possibility to add soft delete on the media space library? Or extend the model in a way to add soft delete I did not find resources in the documentation I use this version https://spatie.be/docs/laravel-medialibrary/v10/introduction

0 likes
3 replies
Niush's avatar

I looked around more, and one Alternative way could be using media collection with strict convention. Extending, model and filesystem only, is not enough in most cases to add soft delete ability.

// 1. Upload Media to 'files' collection:
$post->addMedia(request()->file('file'))->toMediaCollection('files');

// 2. When Fetching Media to show. Only load 'files' collection:
$post->getMedia('files');

// 3. When Deleting, perform soft delete. By moving to 'trash' collection:
$file = $post->getMedia('files')->first();
$file->move($post, 'trash');

// 4. Later, you can also easily clear 'trash' collection at once using:
$post->getMedia('trash'); // Gets all the trashed media
$post->clearMediaCollection('trash');

4 likes

Please or to participate in this conversation.