protected static function boot()
{
parent::boot();
self::creating(function ($model) {
$model->unique_id = self::generateUniqueId();
});
self::deleting(function (Submission $submission) {
Log::info('Deleting model:', ['model' => $submission]);
dd($submission);
// Delete all associated media
$submission->clearMediaCollection('files');
$submission->clearMediaCollection('images');
});
}
i have this method in my submission model the creating works on each new model while creating but the deleting is not working it just deletes the model and this doesnt works
my submission model
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
use IlluminateSupportFacadesLog;
use SpatieMediaLibraryHasMedia;
use SpatieMediaLibraryInteractsWithMedia;
use SpatieMediaLibraryMediaCollectionsFile;
class Submission extends Model implements HasMedia
{
use InteractsWithMedia, HasFactory;
protected $fillable = [
'unique_id',
'user_id',
'title',
'description',
'status',
];
protected static function boot()
{
parent::boot();
self::creating(function ($model) {
$model->unique_id = self::generateUniqueId();
});
self::deleting(function (Submission $submission) {
Log::info('Deleting model:', ['model' => $submission]);
dd($submission);
// Delete all associated media
$submission->clearMediaCollection('files');
$submission->clearMediaCollection('images');
});
}
public static function generateUniqueId()
{
do {
$uniqueId = str_pad(rand(0, 99999999), 12, '0', STR_PAD_LEFT);
} while (self::where('unique_id', $uniqueId)->exists());
return $uniqueId;
}
public function user()
{
return $this->belongsTo(User::class);
}
public function replies()
{
return $this->hasMany(SubmissionReply::class);
}
public function registerMediaCollections(): void
{
$this->addMediaCollection('files')
->useDisk('submissions')
->acceptsFile(function (File $file) {
return in_array($file->mimeType, [
'image/jpeg',
'image/png',
'application/pdf',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'text/plain'
]);
});
$this->addMediaCollection('images')
->useDisk('submissions')
->singleFile()
->acceptsMimeTypes([
'image/jpeg',
'image/png',
'image/jpg',
]);
}
}
2
Answers
The issue is likely due to the use of
dd($submission)
, which halts execution and prevents the media from being deleted. To fix this, removedd()
and add proper logging to ensure the deleting event is triggered and the media is cleared.if you’re using
forceDelete()
, thedeleting
event won’t be triggered. In that case, listen for theforceDeleted
event insteadPlease follow and Update in your code with that basic softDelete solution:
Add Soft Deletes Trait (if you use soft deletes):
use IlluminateDatabaseEloquentSoftDeletes;
class Submission extends Model implements HasMedia
{
use InteractsWithMedia, HasFactory, SoftDeletes;
}
Just import rest of code will be same as before.
Use forceDelete() to delete permanently:
Submission::find($id)->forceDelete();
Clear Cache:
php artisan cache:clear
php artisan config:cache
php artisan route:cache
php artisan view:clear
Verify the Delete Operation in Code:
$submission = Submission::find($id);
$submission->delete();