Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
Last active July 22, 2020 19:52
Show Gist options
  • Save kmuenkel/dd3c0d2ef5a5d721c581c1d73a575755 to your computer and use it in GitHub Desktop.
Save kmuenkel/dd3c0d2ef5a5d721c581c1d73a575755 to your computer and use it in GitHub Desktop.
Customize the nature of the restored and deleted values of a Laravel soft-delete field.
<?php
namespace App\Overrides\Illuminate\Database\Eloquent;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Concerns\HasEvents;
use Illuminate\Database\Eloquent\SoftDeletes as BaseSoftDeletes;
/**
* Trait SoftDeletes
* All methods overridden here are to allow for custom deleted and restored values
*
* @package App\Overrides\Illuminate\Database\Eloquent
* @mixin HasEvents
* @mixin Model
*/
trait SoftDeletes
{
use BaseSoftDeletes;
/**
* Initialize the soft deleting trait for an instance.
*
* @return void
*/
public function initializeSoftDeletes()
{
if ($this->getDeletedType() == 'date') {
$this->dates[] = $this->getDeletedAtColumn();
}
}
/**
* Perform the actual delete query on this model instance.
*
* @return void
*/
protected function runSoftDelete()
{
$query = $this->setKeysForSaveQuery($this->newModelQuery());
$time = $this->freshTimestamp();
$deletedValue = $this->getDeletedValue($time);
$deletedType = $this->getDeletedType();
$columnAssignment = $deletedType == 'date' ? $this->fromDateTime($deletedValue) : $deletedValue;
$columns = [$this->getDeletedAtColumn() => $columnAssignment];
$this->{$this->getDeletedAtColumn()} = $deletedValue;
if ($this->timestamps && !is_null($this->getUpdatedAtColumn())) {
$this->{$this->getUpdatedAtColumn()} = $time;
$columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
}
$query->update($columns);
$this->syncOriginalAttributes(array_keys($columns));
}
/**
* Restore a soft-deleted model instance.
*
* @return bool|null
*/
public function restore()
{
if ($this->fireModelEvent('restoring') === false) {
return false;
}
$this->{$this->getDeletedAtColumn()} = $this->getRestoredValue();
$this->exists = true;
$result = $this->save();
$this->fireModelEvent('restored', false);
return $result;
}
/**
* Determine if the model instance has been soft-deleted.
*
* @return bool
*/
public function trashed()
{
return $this->{$this->getDeletedAtColumn()} !== $this->getRestoredValue();
}
/**
* @return string
*/
public function getDeletedType()
{
$deletedValue = $this->getDeletedValue();
$defaultType = $deletedValue instanceof Carbon ? 'date' : gettype($deletedValue);
return defined('static::DELETED_TYPE') ? static::DELETED_TYPE : $defaultType;
}
/**
* @return mixed|null
*/
public function getRestoredValue()
{
return defined('static::RESTORED_VALUE') ? static::RESTORED_VALUE : null;
}
/**
* @param Carbon|null $time
* @return mixed|Carbon|null
*/
public function getDeletedValue(Carbon $time = null)
{
$time = $time ?: $this->freshTimestamp();
$defaultValue = $this->getDeletedType() == 'date' ? $time : null;
return defined('static::DELETED_VALUE') ? static::DELETED_VALUE : $defaultValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment