Skip to content

Instantly share code, notes, and snippets.

@mirzabusatlic
Created June 15, 2016 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mirzabusatlic/7a7be16ce45d1372e9dad6f94be8e846 to your computer and use it in GitHub Desktop.
Save mirzabusatlic/7a7be16ce45d1372e9dad6f94be8e846 to your computer and use it in GitHub Desktop.
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