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',
]);
}
}