Skip to content

Commit

Permalink
✨ (Model): 重写 SoftDeletes ,支持时间戳
Browse files Browse the repository at this point in the history
  • Loading branch information
Joycezhangw committed Mar 13, 2024
1 parent a6a42d6 commit 7c7393a
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/Model/Concerns/SoftDeletesEx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace JoyceZ\LaravelLib\Model\Concerns;

use Illuminate\Database\Eloquent\SoftDeletes;

trait SoftDeletesEx
{
use SoftDeletes;

public static function bootSoftDeletes()
{
static::addGlobalScope(new SoftDeletingScopeEx());
}


/**
* Perform the actual delete query on this model instance.
*
* @return void
*/
protected function runSoftDelete()
{
$query = $this->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);
}
}
93 changes: 93 additions & 0 deletions src/Model/Concerns/SoftDeletingScopeEx.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

namespace JoyceZ\LaravelLib\Model\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class SoftDeletingScopeEx extends SoftDeletingScope
{
/**
* 将约束加到 Eloquent 查询构造中,这样默认查询的就是 `is_deleted` = 0 的记录了
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model) {
$builder->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;
});
}
}

0 comments on commit 7c7393a

Please sign in to comment.