Просмотр файла vendor/illuminate/database/Eloquent/SoftDeletes.php

Размер файла: 5.05Kb
  1. <?php
  2.  
  3. namespace Illuminate\Database\Eloquent;
  4.  
  5. /**
  6. * @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withTrashed()
  7. * @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder onlyTrashed()
  8. * @method static static|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder withoutTrashed()
  9. */
  10. trait SoftDeletes
  11. {
  12. /**
  13. * Indicates if the model is currently force deleting.
  14. *
  15. * @var bool
  16. */
  17. protected $forceDeleting = false;
  18.  
  19. /**
  20. * Boot the soft deleting trait for a model.
  21. *
  22. * @return void
  23. */
  24. public static function bootSoftDeletes()
  25. {
  26. static::addGlobalScope(new SoftDeletingScope);
  27. }
  28.  
  29. /**
  30. * Initialize the soft deleting trait for an instance.
  31. *
  32. * @return void
  33. */
  34. public function initializeSoftDeletes()
  35. {
  36. if (! isset($this->casts[$this->getDeletedAtColumn()])) {
  37. $this->casts[$this->getDeletedAtColumn()] = 'datetime';
  38. }
  39. }
  40.  
  41. /**
  42. * Force a hard delete on a soft deleted model.
  43. *
  44. * @return bool|null
  45. */
  46. public function forceDelete()
  47. {
  48. $this->forceDeleting = true;
  49.  
  50. return tap($this->delete(), function ($deleted) {
  51. $this->forceDeleting = false;
  52.  
  53. if ($deleted) {
  54. $this->fireModelEvent('forceDeleted', false);
  55. }
  56. });
  57. }
  58.  
  59. /**
  60. * Perform the actual delete query on this model instance.
  61. *
  62. * @return mixed
  63. */
  64. protected function performDeleteOnModel()
  65. {
  66. if ($this->forceDeleting) {
  67. $this->exists = false;
  68.  
  69. return $this->setKeysForSaveQuery($this->newModelQuery())->forceDelete();
  70. }
  71.  
  72. return $this->runSoftDelete();
  73. }
  74.  
  75. /**
  76. * Perform the actual delete query on this model instance.
  77. *
  78. * @return void
  79. */
  80. protected function runSoftDelete()
  81. {
  82. $query = $this->setKeysForSaveQuery($this->newModelQuery());
  83.  
  84. $time = $this->freshTimestamp();
  85.  
  86. $columns = [$this->getDeletedAtColumn() => $this->fromDateTime($time)];
  87.  
  88. $this->{$this->getDeletedAtColumn()} = $time;
  89.  
  90. if ($this->timestamps && ! is_null($this->getUpdatedAtColumn())) {
  91. $this->{$this->getUpdatedAtColumn()} = $time;
  92.  
  93. $columns[$this->getUpdatedAtColumn()] = $this->fromDateTime($time);
  94. }
  95.  
  96. $query->update($columns);
  97.  
  98. $this->syncOriginalAttributes(array_keys($columns));
  99. }
  100.  
  101. /**
  102. * Restore a soft-deleted model instance.
  103. *
  104. * @return bool|null
  105. */
  106. public function restore()
  107. {
  108. // If the restoring event does not return false, we will proceed with this
  109. // restore operation. Otherwise, we bail out so the developer will stop
  110. // the restore totally. We will clear the deleted timestamp and save.
  111. if ($this->fireModelEvent('restoring') === false) {
  112. return false;
  113. }
  114.  
  115. $this->{$this->getDeletedAtColumn()} = null;
  116.  
  117. // Once we have saved the model, we will fire the "restored" event so this
  118. // developer will do anything they need to after a restore operation is
  119. // totally finished. Then we will return the result of the save call.
  120. $this->exists = true;
  121.  
  122. $result = $this->save();
  123.  
  124. $this->fireModelEvent('restored', false);
  125.  
  126. return $result;
  127. }
  128.  
  129. /**
  130. * Determine if the model instance has been soft-deleted.
  131. *
  132. * @return bool
  133. */
  134. public function trashed()
  135. {
  136. return ! is_null($this->{$this->getDeletedAtColumn()});
  137. }
  138.  
  139. /**
  140. * Register a "restoring" model event callback with the dispatcher.
  141. *
  142. * @param \Closure|string $callback
  143. * @return void
  144. */
  145. public static function restoring($callback)
  146. {
  147. static::registerModelEvent('restoring', $callback);
  148. }
  149.  
  150. /**
  151. * Register a "restored" model event callback with the dispatcher.
  152. *
  153. * @param \Closure|string $callback
  154. * @return void
  155. */
  156. public static function restored($callback)
  157. {
  158. static::registerModelEvent('restored', $callback);
  159. }
  160.  
  161. /**
  162. * Register a "forceDeleted" model event callback with the dispatcher.
  163. *
  164. * @param \Closure|string $callback
  165. * @return void
  166. */
  167. public static function forceDeleted($callback)
  168. {
  169. static::registerModelEvent('forceDeleted', $callback);
  170. }
  171.  
  172. /**
  173. * Determine if the model is currently force deleting.
  174. *
  175. * @return bool
  176. */
  177. public function isForceDeleting()
  178. {
  179. return $this->forceDeleting;
  180. }
  181.  
  182. /**
  183. * Get the name of the "deleted at" column.
  184. *
  185. * @return string
  186. */
  187. public function getDeletedAtColumn()
  188. {
  189. return defined('static::DELETED_AT') ? static::DELETED_AT : 'deleted_at';
  190. }
  191.  
  192. /**
  193. * Get the fully qualified "deleted at" column.
  194. *
  195. * @return string
  196. */
  197. public function getQualifiedDeletedAtColumn()
  198. {
  199. return $this->qualifyColumn($this->getDeletedAtColumn());
  200. }
  201. }