ApiSoftDeletingScope.php 4.5 KB

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