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
MassPrunable.php
42 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED\Illuminate\Database\Eloquent; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Database\Events\ModelsPruned; |
| 6 | use LogicException; |
| 7 | /** @internal */ |
| 8 | trait MassPrunable |
| 9 | { |
| 10 | /** |
| 11 | * Prune all prunable models in the database. |
| 12 | * |
| 13 | * @param int $chunkSize |
| 14 | * @return int |
| 15 | */ |
| 16 | public function pruneAll(int $chunkSize = 1000) |
| 17 | { |
| 18 | $query = \IAWPSCOPED\tap($this->prunable(), function ($query) use($chunkSize) { |
| 19 | $query->when(!$query->getQuery()->limit, function ($query) use($chunkSize) { |
| 20 | $query->limit($chunkSize); |
| 21 | }); |
| 22 | }); |
| 23 | $total = 0; |
| 24 | do { |
| 25 | $total += $count = \in_array(SoftDeletes::class, class_uses_recursive(\get_class($this))) ? $query->forceDelete() : $query->delete(); |
| 26 | if ($count > 0) { |
| 27 | event(new ModelsPruned(static::class, $total)); |
| 28 | } |
| 29 | } while ($count > 0); |
| 30 | return $total; |
| 31 | } |
| 32 | /** |
| 33 | * Get the prunable model query. |
| 34 | * |
| 35 | * @return \Illuminate\Database\Eloquent\Builder |
| 36 | */ |
| 37 | public function prunable() |
| 38 | { |
| 39 | throw new LogicException('Please implement the prunable method on your model.'); |
| 40 | } |
| 41 | } |
| 42 |