Skip to content

Commit 7c7393a

Browse files
committed
✨ (Model): 重写 SoftDeletes ,支持时间戳
1 parent a6a42d6 commit 7c7393a

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace JoyceZ\LaravelLib\Model\Concerns;
4+
5+
use Illuminate\Database\Eloquent\SoftDeletes;
6+
7+
trait SoftDeletesEx
8+
{
9+
use SoftDeletes;
10+
11+
public static function bootSoftDeletes()
12+
{
13+
static::addGlobalScope(new SoftDeletingScopeEx());
14+
}
15+
16+
17+
/**
18+
* Perform the actual delete query on this model instance.
19+
*
20+
* @return void
21+
*/
22+
protected function runSoftDelete()
23+
{
24+
$query = $this->newQueryWithoutScopes()->where($this->getKeyName(), $this->getKey());
25+
26+
// 0. 正常 1. 已删除
27+
$this->{$this->getDeletedAtColumn()} = now()->timestamp;
28+
29+
$query->update([
30+
$this->getDeletedAtColumn() => now()->timestamp
31+
]);
32+
}
33+
34+
/**
35+
* Restore a soft-deleted model instance.
36+
*
37+
* @return bool|null
38+
*/
39+
public function restore()
40+
{
41+
// If the restoring event does not return false, we will proceed with this
42+
// restore operation. Otherwise, we bail out so the developer will stop
43+
// the restore totally. We will clear the deleted timestamp and save.
44+
if ($this->fireModelEvent('restoring') === false) {
45+
return false;
46+
}
47+
48+
$this->{$this->getDeletedAtColumn()} = 0;
49+
50+
// Once we have saved the model, we will fire the "restored" event so this
51+
// developer will do anything they need to after a restore operation is
52+
// totally finished. Then we will return the result of the save call.
53+
$this->exists = true;
54+
55+
$result = $this->save();
56+
57+
$this->fireModelEvent('restored', false);
58+
59+
return $result;
60+
}
61+
62+
/**
63+
* Determine if the model instance has been soft-deleted.
64+
*
65+
* @return bool
66+
*/
67+
public function trashed()
68+
{
69+
return !($this->{$this->getDeletedAtColumn()} === 0);
70+
}
71+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
namespace JoyceZ\LaravelLib\Model\Concerns;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Illuminate\Database\Eloquent\SoftDeletingScope;
8+
9+
class SoftDeletingScopeEx extends SoftDeletingScope
10+
{
11+
/**
12+
* 将约束加到 Eloquent 查询构造中,这样默认查询的就是 `is_deleted` = 0 的记录了
13+
* Apply the scope to a given Eloquent query builder.
14+
*
15+
* @param \Illuminate\Database\Eloquent\Builder $builder
16+
* @param \Illuminate\Database\Eloquent\Model $model
17+
* @return void
18+
*/
19+
public function apply(Builder $builder, Model $model) {
20+
$builder->where($model->getQualifiedDeletedAtColumn(), 0);
21+
}
22+
23+
/**
24+
* Extend the query builder with the needed functions.
25+
*
26+
* @param \Illuminate\Database\Eloquent\Builder $builder
27+
* @return void
28+
*/
29+
public function extend(Builder $builder) {
30+
foreach ($this->extensions as $extension) {
31+
$this->{"add{$extension}"}($builder);
32+
}
33+
34+
$builder->onDelete(function (Builder $builder) {
35+
$column = $this->getDeletedAtColumn($builder);
36+
37+
return $builder->update([
38+
$column => \DB::Raw('UNIX_TIMESTAMP(NOW())')
39+
]);
40+
});
41+
}
42+
43+
/**
44+
* Add the restore extension to the builder.
45+
*
46+
* @param \Illuminate\Database\Eloquent\Builder $builder
47+
* @return void
48+
*/
49+
protected function addRestore(Builder $builder) {
50+
$builder->macro('restore', function (Builder $builder) {
51+
$builder->withTrashed();
52+
53+
return $builder->update([
54+
$builder->getModel()
55+
->getDeletedAtColumn() => 0
56+
]);
57+
});
58+
}
59+
60+
/**
61+
* Add the without-trashed extension to the builder.
62+
*
63+
* @param \Illuminate\Database\Eloquent\Builder $builder
64+
* @return void
65+
*/
66+
protected function addWithoutTrashed(Builder $builder) {
67+
$builder->macro('withoutTrashed', function (Builder $builder) {
68+
$model = $builder->getModel();
69+
70+
$builder->withoutGlobalScope($this)
71+
->where($model->getQualifiedDeletedAtColumn(), 0);
72+
73+
return $builder;
74+
});
75+
}
76+
77+
/**
78+
* Add the only-trashed extension to the builder.
79+
*
80+
* @param \Illuminate\Database\Eloquent\Builder $builder
81+
* @return void
82+
*/
83+
protected function addOnlyTrashed(Builder $builder) {
84+
$builder->macro('onlyTrashed', function (Builder $builder) {
85+
$model = $builder->getModel();
86+
87+
$builder->withoutGlobalScope($this)
88+
->where($model->getQualifiedDeletedAtColumn(), '<>', 0);
89+
90+
return $builder;
91+
});
92+
}
93+
}

0 commit comments

Comments
 (0)