diff --git a/src/Model/Concerns/SoftDeletesEx.php b/src/Model/Concerns/SoftDeletesEx.php new file mode 100644 index 0000000..6eb8f03 --- /dev/null +++ b/src/Model/Concerns/SoftDeletesEx.php @@ -0,0 +1,71 @@ +newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey()); + + // 0. 正常 1. 已删除 + $this->{$this->getDeletedAtColumn()} = now()->timestamp; + + $query->update([ + $this->getDeletedAtColumn() => now()->timestamp + ]); + } + + /** + * Restore a soft-deleted model instance. + * + * @return bool|null + */ + public function restore() + { + // If the restoring event does not return false, we will proceed with this + // restore operation. Otherwise, we bail out so the developer will stop + // the restore totally. We will clear the deleted timestamp and save. + if ($this->fireModelEvent('restoring') === false) { + return false; + } + + $this->{$this->getDeletedAtColumn()} = 0; + + // Once we have saved the model, we will fire the "restored" event so this + // developer will do anything they need to after a restore operation is + // totally finished. Then we will return the result of the save call. + $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()} === 0); + } +} \ No newline at end of file diff --git a/src/Model/Concerns/SoftDeletingScopeEx.php b/src/Model/Concerns/SoftDeletingScopeEx.php new file mode 100644 index 0000000..7db23be --- /dev/null +++ b/src/Model/Concerns/SoftDeletingScopeEx.php @@ -0,0 +1,93 @@ +where($model->getQualifiedDeletedAtColumn(), 0); + } + + /** + * Extend the query builder with the needed functions. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + public function extend(Builder $builder) { + foreach ($this->extensions as $extension) { + $this->{"add{$extension}"}($builder); + } + + $builder->onDelete(function (Builder $builder) { + $column = $this->getDeletedAtColumn($builder); + + return $builder->update([ + $column => \DB::Raw('UNIX_TIMESTAMP(NOW())') + ]); + }); + } + + /** + * Add the restore extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addRestore(Builder $builder) { + $builder->macro('restore', function (Builder $builder) { + $builder->withTrashed(); + + return $builder->update([ + $builder->getModel() + ->getDeletedAtColumn() => 0 + ]); + }); + } + + /** + * Add the without-trashed extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addWithoutTrashed(Builder $builder) { + $builder->macro('withoutTrashed', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this) + ->where($model->getQualifiedDeletedAtColumn(), 0); + + return $builder; + }); + } + + /** + * Add the only-trashed extension to the builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @return void + */ + protected function addOnlyTrashed(Builder $builder) { + $builder->macro('onlyTrashed', function (Builder $builder) { + $model = $builder->getModel(); + + $builder->withoutGlobalScope($this) + ->where($model->getQualifiedDeletedAtColumn(), '<>', 0); + + return $builder; + }); + } +} \ No newline at end of file