namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;
trait CustomSoftDeletes
{
public static function bootCustomSoftDeletes()
{
static::addGlobalScope(new SoftDeletingScope);
}
public function forceDelete()
{
return tap($this->runSoftDelete(), function ($instance) {
$instance->delete();
});
}
public function restore()
{
if ($this->fireModelEvent('restoring') === false) {
return false;
}
$this->{$this->getDeletedAtColumn()} = 0;
$this->exists = true;
$this->fireModelEvent('restored', false);
return $this->save();
}
public function trashed()
{
return ! is_null($this->{$this->getDeletedAtColumn()});
}
protected function runSoftDelete()
{
$query = $this->newModelQuery()->where($this->getKeyName(), $this->getKey());
$this->{$this->getDeletedAtColumn()} = 1;
$query->update([$this->getDeletedAtColumn() => $this->freshTimestampString()]);
$this->syncOriginal();
$this->fireModelEvent('deleted', false);
}
public function getDeletedAtColumn()
{
return defined('static::DELETED_AT') ? static::DELETED_AT : 'is_deleted';
}
public function getQualifiedDeletedAtColumn()
{
return $this->qualifyColumn($this->getDeletedAtColumn());
}
}