Option on Media to preserve files when deleted #2005
-
This suggestion is easiest to explain with a situation that just happened to me:
What I am suggesting is that an option is added to Based on this... ...this can already be accomplished by using my own This would require a new property on A corresponding option could be added to a media collection, effectively setting the option on all I am happy to PR the entire change, but I wanted to propose it and get approval before I take the time. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Thanks for your idea, but I'm going to leave things as they are for now. I think that |
Beta Was this translation helpful? Give feedback.
-
@freekmurze thank you for considering it! I was able to accomplish what I was after by using my own model and adding soft deletes. What would you think about adding some info about this to the "Using your own model" docs page? I can write it up and PR it if you'd like. I'll put these here for anybody that comes across this discussion:
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;
class Media extends BaseMedia
{
use SoftDeletes;
}
class AddSoftDeletesToMedia extends Migration
{
public function up(): void
{
Schema::table('media', function (Blueprint $table) {
$table->softDeletes();
});
}
public function down(): void
{
Schema::table('media', function (Blueprint $table) {
$table->dropSoftDeletes();
});
}
} |
Beta Was this translation helpful? Give feedback.
-
For anybody that implements soft deletes on their own
The last step causes a problem because Since the temporary-upload To get around this, you have to force-delete the temporary-upload use Spatie\MediaLibraryPro\Models\TemporaryUpload;
protected static function booted(): void
{
static::deleted(fn (Media $media) => $media->forceDeleteIfTemporaryMediaLibraryUpload());
}
public function forceDeleteIfTemporaryMediaLibraryUpload(): void
{
if ($this->model instanceof TemporaryUpload && ! $this->isForceDeleting()) {
$this->forceDelete();
}
} |
Beta Was this translation helpful? Give feedback.
-
Adding to @squatto's issue, when using the single file collection, the existing media gets deleted along with its corresponding database row and a new media row is created with a new auto incrementing id. Isn't this an issue as with every update we lose a valuable auto incrementing id in the media table? Nevertheless to say, the old id will be invalid as well making some links inoperable where it shouldn't have. My intention is to replace a media in the collection, not to create another media and delete the last one. @freekmurze please let me know if you think this should be implemented for single file collection or any suggestion where it shouldn't be. 🙏🏻 |
Beta Was this translation helpful? Give feedback.
Thanks for your idea, but I'm going to leave things as they are for now. I think that
deletePreservingMedia
is enough to cover this functionality for most users.