-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6a42d6
commit 7c7393a
Showing
2 changed files
with
164 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |