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