SoftDeletingScope.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. class SoftDeletingScope implements Scope
  4. {
  5. /**
  6. * All of the extensions to be added to the builder.
  7. *
  8. * @var array
  9. */
  10. protected $extensions = ['Restore', 'WithTrashed', 'WithoutTrashed', 'OnlyTrashed'];
  11. /**
  12. * Apply the scope to a given Eloquent query builder.
  13. *
  14. * @param \Illuminate\Database\Eloquent\Builder $builder
  15. * @param \Illuminate\Database\Eloquent\Model $model
  16. * @return void
  17. */
  18. public function apply(Builder $builder, Model $model)
  19. {
  20. $builder->whereNull($model->getQualifiedDeletedAtColumn());
  21. }
  22. /**
  23. * Extend the query builder with the needed functions.
  24. *
  25. * @param \Illuminate\Database\Eloquent\Builder $builder
  26. * @return void
  27. */
  28. public function extend(Builder $builder)
  29. {
  30. foreach ($this->extensions as $extension) {
  31. $this->{"add{$extension}"}($builder);
  32. }
  33. $builder->onDelete(function (Builder $builder) {
  34. $column = $this->getDeletedAtColumn($builder);
  35. return $builder->update([
  36. $column => $builder->getModel()->freshTimestampString(),
  37. ]);
  38. });
  39. }
  40. /**
  41. * Get the "deleted at" column for the builder.
  42. *
  43. * @param \Illuminate\Database\Eloquent\Builder $builder
  44. * @return string
  45. */
  46. protected function getDeletedAtColumn(Builder $builder)
  47. {
  48. if (count((array) $builder->getQuery()->joins) > 0) {
  49. return $builder->getModel()->getQualifiedDeletedAtColumn();
  50. }
  51. return $builder->getModel()->getDeletedAtColumn();
  52. }
  53. /**
  54. * Add the restore extension to the builder.
  55. *
  56. * @param \Illuminate\Database\Eloquent\Builder $builder
  57. * @return void
  58. */
  59. protected function addRestore(Builder $builder)
  60. {
  61. $builder->macro('restore', function (Builder $builder) {
  62. $builder->withTrashed();
  63. return $builder->update([$builder->getModel()->getDeletedAtColumn() => null]);
  64. });
  65. }
  66. /**
  67. * Add the with-trashed extension to the builder.
  68. *
  69. * @param \Illuminate\Database\Eloquent\Builder $builder
  70. * @return void
  71. */
  72. protected function addWithTrashed(Builder $builder)
  73. {
  74. $builder->macro('withTrashed', function (Builder $builder, $withTrashed = true) {
  75. if (! $withTrashed) {
  76. return $builder->withoutTrashed();
  77. }
  78. return $builder->withoutGlobalScope($this);
  79. });
  80. }
  81. /**
  82. * Add the without-trashed extension to the builder.
  83. *
  84. * @param \Illuminate\Database\Eloquent\Builder $builder
  85. * @return void
  86. */
  87. protected function addWithoutTrashed(Builder $builder)
  88. {
  89. $builder->macro('withoutTrashed', function (Builder $builder) {
  90. $model = $builder->getModel();
  91. $builder->withoutGlobalScope($this)->whereNull(
  92. $model->getQualifiedDeletedAtColumn()
  93. );
  94. return $builder;
  95. });
  96. }
  97. /**
  98. * Add the only-trashed extension to the builder.
  99. *
  100. * @param \Illuminate\Database\Eloquent\Builder $builder
  101. * @return void
  102. */
  103. protected function addOnlyTrashed(Builder $builder)
  104. {
  105. $builder->macro('onlyTrashed', function (Builder $builder) {
  106. $model = $builder->getModel();
  107. $builder->withoutGlobalScope($this)->whereNotNull(
  108. $model->getQualifiedDeletedAtColumn()
  109. );
  110. return $builder;
  111. });
  112. }
  113. }