SoftDeletes.php 4.0 KB

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