Instantly share code, notes, and snippets.

Embed
What would you like to do?
Classifies between soft/hard deletion in model observers in Laravel
<?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