ApiSoftDeletingScope.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. * 软删除 socpe
  4. */
  5. namespace App\Base\Models;
  6. use Illuminate\Database\Eloquent\SoftDeletingScope;
  7. use Illuminate\Database\Eloquent\Builder;
  8. use Illuminate\Database\Eloquent\Model;
  9. class ApiSoftDeletingScope extends SoftDeletingScope
  10. {
  11. /**
  12. * All of the extensions to be added to the builder.
  13. * restore:恢复
  14. * WithTrashed:带有删除状态和正常状态的数据
  15. * OnlyTrashed:只有删除状态的数据
  16. * WithDisabled:带有禁用状态的数据 即正常状态+禁用状态
  17. * OnlyDisabled:只查询出禁用状态
  18. * WithAll:所有
  19. * @var array
  20. */
  21. protected $extensions = ['Restore', 'WithTrashed', 'OnlyTrashed','WithDisabled','OnlyDisabled','WithAll'];
  22. /**
  23. * Apply the scope to a given Eloquent query builder.
  24. *
  25. * @param \Illuminate\Database\Eloquent\Builder $builder
  26. * @param \Illuminate\Database\Eloquent\Model $model
  27. * @return void
  28. */
  29. public function apply(Builder $builder, Model $model)
  30. {
  31. if(is_null($model::STATUS_ENABLED)){
  32. $builder->whereNull($model->getQualifiedDeletedAtColumn());
  33. }else{
  34. // $builder->where($model->getQualifiedDeletedAtColumn(),$model::STATUS_ENABLED);
  35. }
  36. }
  37. /**
  38. * Extend the query builder with the needed functions.
  39. *
  40. * @param \Illuminate\Database\Eloquent\Builder $builder
  41. * @return void
  42. */
  43. public function extend(Builder $builder)
  44. {
  45. foreach ($this->extensions as $extension) {
  46. $this->{"add{$extension}"}($builder);
  47. }
  48. $builder->onDelete(function (Builder $builder) {
  49. $column = $this->getDeletedAtColumn($builder);
  50. $model = $builder->getModel();
  51. return $builder->update([
  52. $column => $model->getDeletedColumnValue(),
  53. ]);
  54. });
  55. }
  56. /**
  57. * Add the restore extension to the builder.
  58. *
  59. * @param \Illuminate\Database\Eloquent\Builder $builder
  60. * @return void
  61. */
  62. protected function addRestore(Builder $builder)
  63. {
  64. $builder->macro('restore', function (Builder $builder) {
  65. $builder->withAll();
  66. $model = $builder->getModel();
  67. return $builder->update([$model->getDeletedAtColumn() => $model::STATUS_ENABLED]);
  68. });
  69. }
  70. /**
  71. * 筛选出正常状态和删除状态的数据
  72. *
  73. * @param \Illuminate\Database\Eloquent\Builder $builder
  74. * @return void
  75. */
  76. protected function addWithTrashed(Builder $builder)
  77. {
  78. $builder->macro('withTrashed', function (Builder $builder) {
  79. $builder->withoutGlobalScope($this);
  80. $model = $builder->getModel();
  81. return $builder->whereIn($model::DELETED_AT,[$model::STATUS_DELETED,$model::STATUS_ENABLED]);
  82. });
  83. }
  84. /**
  85. * 只筛选出删除的数据
  86. *
  87. * @param \Illuminate\Database\Eloquent\Builder $builder
  88. * @return void
  89. */
  90. protected function addOnlyTrashed(Builder $builder)
  91. {
  92. $builder->macro('onlyTrashed', function (Builder $builder) {
  93. $model = $builder->getModel();
  94. $builder->withoutGlobalScope($this)->where(
  95. $model->getQualifiedDeletedAtColumn(),$model::STATUS_DELETED
  96. );
  97. return $builder;
  98. });
  99. }
  100. /**
  101. * 筛选出正常状态和禁用状态的数据
  102. * @param Builder $builder
  103. */
  104. protected function addWithDisabled(Builder $builder){
  105. $builder->macro('withDisabled', function (Builder $builder) {
  106. $model = $builder->getModel();
  107. $builder->withoutGlobalScope($this)->whereIn(
  108. $model->getQualifiedDeletedAtColumn(),[$model::STATUS_DISABLED,$model::STATUS_ENABLED]
  109. );
  110. return $builder;
  111. });
  112. }
  113. /**
  114. * 只筛选出禁用状态的数据
  115. * @param Builder $builder
  116. */
  117. protected function addOnlyDisabled(Builder $builder){
  118. $builder->macro('onlyDisabled', function (Builder $builder) {
  119. $model = $builder->getModel();
  120. $builder->withoutGlobalScope($this)->where(
  121. $model->getQualifiedDeletedAtColumn(),$model::STATUS_DISABLED
  122. );
  123. return $builder;
  124. });
  125. }
  126. /**
  127. * 筛选出所有数据
  128. * @param Builder $builder
  129. */
  130. protected function addWithAll(Builder $builder){
  131. $builder->macro('withAll', function (Builder $builder) {
  132. return $builder->withoutGlobalScope($this);
  133. });
  134. }
  135. }