SoftDeletes.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace Illuminate\Database\Eloquent;
  3. trait SoftDeletes
  4. {
  5. /**
  6. * Indicates if the model is currently force deleting.
  7. *
  8. * @var bool
  9. */
  10. protected $forceDeleting = false;
  11. /**
  12. * Boot the soft deleting trait for a model.
  13. *
  14. * @return void
  15. */
  16. public static function bootSoftDeletes()
  17. {
  18. static::addGlobalScope(new SoftDeletingScope);
  19. }
  20. /**
  21. * Force a hard delete on a soft deleted model.
  22. *
  23. * @return bool|null
  24. */
  25. public function forceDelete()
  26. {
  27. $this->forceDeleting = true;
  28. return tap($this->delete(), function ($deleted) {
  29. $this->forceDeleting = false;
  30. if ($deleted) {
  31. $this->fireModelEvent('forceDeleted', false);
  32. }
  33. });
  34. }
  35. /**
  36. * Perform the actual delete query on this model instance.
  37. *
  38. * @return mixed
  39. */
  40. protected function performDeleteOnModel()
  41. {
  42. if ($this->forceDeleting) {
  43. $this->exists = false;
  44. return $this->newModelQuery()->where($this->getKeyName(), $this->getKey())->forceDelete();
  45. }
  46. return $this->runSoftDelete();
  47. }
  48. /**
  49. * Perform the actual delete query on this model instance.
  50. *
  51. * @return void
  52. */
  53. protected function runSoftDelete()
  54. {
  55. $query = $this->newModelQuery()->where($this->getKeyName(), $this->getKey());
  56. $time = $this->freshTimestamp();
  57. $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
  58. $this->{$this->getDeletedAtColumn()} = $time;
  59. if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
  60. $this->{$this->getUpdatedAtColumn()} = $time;
  61. $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
  62. }
  63. if ($query->update($columns)) {
  64. $this->syncOriginal();
  65. }
  66. }
  67. /**
  68. * Restore a soft-deleted model instance.
  69. *
  70. * @return bool|null
  71. */
  72. public function restore()
  73. {
  74. // If the restoring event does not return false, we will proceed with this
  75. // restore operation. Otherwise, we bail out so the developer will stop
  76. // the restore totally. We will clear the deleted timestamp and save.
  77. if ($this->fireModelEvent('restoring') === false) {
  78. return false;
  79. }
  80. $this->{$this->getDeletedAtColumn()} = null;
  81. // Once we have saved the model, we will fire the "restored" event so this
  82. // developer will do anything they need to after a restore operation is
  83. // totally finished. Then we will return the result of the save call.
  84. $this->exists = true;
  85. $result = $this->save();
  86. $this->fireModelEvent('restored', false);
  87. return $result;
  88. }
  89. /**
  90. * Determine if the model instance has been soft-deleted.
  91. *
  92. * @return bool
  93. */
  94. public function trashed()
  95. {
  96. return ! is_null($this->{$this->getDeletedAtColumn()});
  97. }
  98. /**
  99. * Register a restoring model event with the dispatcher.
  100. *
  101. * @param \Closure|string $callback
  102. * @return void
  103. */
  104. public static function restoring($callback)
  105. {
  106. static::registerModelEvent('restoring', $callback);
  107. }
  108. /**
  109. * Register a restored model event with the dispatcher.
  110. *
  111. * @param \Closure|string $callback
  112. * @return void
  113. */
  114. public static function restored($callback)
  115. {
  116. static::registerModelEvent('restored', $callback);
  117. }
  118. /**
  119. * Determine if the model is currently force deleting.
  120. *
  121. * @return bool
  122. */
  123. public function isForceDeleting()
  124. {
  125. return $this->forceDeleting;
  126. }
  127. /**
  128. * Get the name of the "deleted at" column.
  129. *
  130. * @return string
  131. */
  132. public function getDeletedAtColumn()
  133. {
  134. return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
  135. }
  136. /**
  137. * Get the fully qualified "deleted at" column.
  138. *
  139. * @return string
  140. */
  141. public function getQualifiedDeletedAtColumn()
  142. {
  143. return $this->qualifyColumn($this->getDeletedAtColumn());
  144. }
  145. }