class WhatEverObserver {
// Runs when model is updated
public function updated(WhatEver $whatEvah) {
// Only trigger is file_path column is changed
if ( $whatEvah->isDirty('file_path') ) {
//Get the previous file_path record
$oldPath = $whatEvah->getOriginal('file_path');
//delete the previous file_path
$whatEvah->deleteS3File( $oldPath );
}
}
// Delete the file_path during model deletion
public function deleting( WhatEver $whatEvah ) {
$whatEvah->deleteS3File( $whatEvah->file_path );
}
}
2条答案
按热度按时间8e2ybdfx1#
只需在模型内部或外部创建一个方法或帮助器,从S3Bucket中删除文件,然后设置一个观察器在更新或删除过程中触发它
即
在你的模型上
然后生成观察者
则可以在
updated
和deleting
事件上调用该方法请务必查看观察员文档
osh3o9ms2#
您可以使用**$调度事件**
创建一个事件,然后在模型中,
看看这个:https://laravel.com/docs/9.x/eloquent#events