Skip to content

Commit 5cdb5ed

Browse files
[8.x] Adds "Prunable" models documentation (#7176)
* Update eloquent.md * Updates title * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 7e4dd52 commit 5cdb5ed

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

eloquent.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [Deleting Models](#deleting-models)
2626
- [Soft Deleting](#soft-deleting)
2727
- [Querying Soft Deleted Models](#querying-soft-deleted-models)
28+
- [Pruning Models](#pruning-models)
2829
- [Replicating Models](#replicating-models)
2930
- [Query Scopes](#query-scopes)
3031
- [Global Scopes](#global-scopes)
@@ -880,6 +881,93 @@ The `onlyTrashed` method will retrieve **only** soft deleted models:
880881
->where('airline_id', 1)
881882
->get();
882883

884+
<a name="pruning-models"></a>
885+
## Pruning Models
886+
887+
Sometimes you may want to periodically delete models that are no longer needed. In those cases, you may add the `Illuminate\Database\Eloquent\Prunable` or `Illuminate\Database\Eloquent\MassPrunable` trait to the models you would like to periodically prune. After adding one of the traits to the model, implement a `prunable` method which returns an Eloquent query builder that resolves the models that are no longer needed:
888+
889+
<?php
890+
891+
namespace App\Models;
892+
893+
use Illuminate\Database\Eloquent\Model;
894+
use Illuminate\Database\Eloquent\Prunable;
895+
896+
class Flight extends Model
897+
{
898+
use Prunable;
899+
900+
/**
901+
* Get the prunable model query.
902+
*
903+
* @return \Illuminate\Database\Eloquent\Builder
904+
*/
905+
public function prunable()
906+
{
907+
return static::where('created_at', '<=', now()->subMonth());
908+
}
909+
}
910+
911+
When marking models as `Prunable`, you may also define a `pruning` method on the model. This method will be called called before the model is deleted. This method can be useful for deleting any additional resources associated with the model, such as stored files, before the model is permanently removed from the database:
912+
913+
/**
914+
* Prepare the model for pruning.
915+
*
916+
* @return void
917+
*/
918+
protected function pruning()
919+
{
920+
//
921+
}
922+
923+
After configuring your prunable model, you should schedule the `model:prune` Artisan command in your application's `App\Console\Kernel` class. You are free to choose the appropriate interval at which this command should be run:
924+
925+
/**
926+
* Define the application's command schedule.
927+
*
928+
* @param \Illuminate\Console\Scheduling\Schedule $schedule
929+
* @return void
930+
*/
931+
protected function schedule(Schedule $schedule)
932+
{
933+
$schedule->command('model:prune')->daily();
934+
}
935+
936+
Behind the scenes, the `model:prune` command will automatically detect "Prunable" models within your application's `app/Models` directory. If your models are in a different location, you may use the `--model` option to specify the model class names:
937+
938+
$schedule->command('model:prune', [
939+
'--model' => [Address::class, Flight::class],
940+
])->daily();
941+
942+
> {note} Soft deleting models will be permanently deleted (`forceDelete`) if they match the prunable query.
943+
944+
<a name="mass-pruning"></a>
945+
#### Mass Pruning
946+
947+
When models are marked with the `Illuminate\Database\Eloquent\MassPrunable` trait, models are deleted from the database using mass-deletion queries. Therefore, the `pruning` method will not be invoked, nor will the `deleting` and `deleted` model events be dispatched. This is because the models are never actually retrieved before deletion, thus making the pruning process much more efficient:
948+
949+
<?php
950+
951+
namespace App\Models;
952+
953+
use Illuminate\Database\Eloquent\Model;
954+
use Illuminate\Database\Eloquent\MassPrunable;
955+
956+
class Flight extends Model
957+
{
958+
use MassPrunable;
959+
960+
/**
961+
* Get the prunable model query.
962+
*
963+
* @return \Illuminate\Database\Eloquent\Builder
964+
*/
965+
public function prunable()
966+
{
967+
return static::where('created_at', '<=', now()->subMonth());
968+
}
969+
}
970+
883971
<a name="replicating-models"></a>
884972
## Replicating Models
885973

0 commit comments

Comments
 (0)