From 779adeafc93d8e6d02832d7bb4b3ff8b6da3f8b4 Mon Sep 17 00:00:00 2001 From: Jochen Sengier Date: Wed, 24 Nov 2021 12:01:47 +0100 Subject: [PATCH] Add optional 'exclude' parameter to exclude auto-detected models from being pruned --- src/Illuminate/Database/Console/PruneCommand.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Illuminate/Database/Console/PruneCommand.php b/src/Illuminate/Database/Console/PruneCommand.php index d84b0a01517f..4661c7590a4a 100644 --- a/src/Illuminate/Database/Console/PruneCommand.php +++ b/src/Illuminate/Database/Console/PruneCommand.php @@ -19,6 +19,7 @@ class PruneCommand extends Command */ protected $signature = 'model:prune {--model=* : Class names of the models to be pruned} + {--exclude=* : Class names of the models to be excluded} {--chunk=1000 : The number of models to retrieve per chunk of models to be deleted} {--pretend : Display the number of prunable records found instead of deleting them}'; @@ -87,6 +88,8 @@ protected function models() return collect($models); } + $exclude = $this->option('exclude'); + return collect((new Finder)->in(app_path('Models'))->files()->name('*.php')) ->map(function ($model) { $namespace = $this->laravel->getNamespace(); @@ -96,6 +99,10 @@ protected function models() ['\\', ''], Str::after($model->getRealPath(), realpath(app_path()).DIRECTORY_SEPARATOR) ); + })->when(! empty($exclude), function ($models) use ($exclude) { + return $models->filter(function ($model) use ($exclude) { + return ! in_array($model, $exclude); + }); })->filter(function ($model) { return $this->isPrunable($model); })->values();