Classifies between soft/hard deletion in model observers in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Watermark\Observers; | |
use Illuminate\Database\Eloquent\Model; | |
trait ClassifiesDeleted | |
{ | |
/** | |
* Hook into post-delete operations. | |
* | |
* @param Model $model | |
*/ | |
public function deleted(Model $model) | |
{ | |
$method = $this->isSoftDeleted($model) ? 'softDeleted' : 'hardDeleted'; | |
// Call the appropriate method, if it exists. | |
if (method_exists($this, $method)) { | |
return $this->$method($model); | |
} | |
} | |
/** | |
* Determine if the model was soft deleted. | |
* | |
* @param Model $model | |
* | |
* @return bool | |
*/ | |
private function isSoftDeleted(Model $model) | |
{ | |
return $model->isDirty($model->getDeletedAtColumn()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment