diff --git a/src/Illuminate/Console/Concerns/FindsAvailableModels.php b/src/Illuminate/Console/Concerns/FindsAvailableModels.php new file mode 100644 index 000000000000..e2a81e62b4f0 --- /dev/null +++ b/src/Illuminate/Console/Concerns/FindsAvailableModels.php @@ -0,0 +1,25 @@ + + */ + protected function findAvailableModels() + { + $modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path(); + + return (new Collection(Finder::create()->files()->depth(0)->in($modelPath))) + ->map(fn ($file) => $file->getBasename('.php')) + ->sort() + ->values() + ->all(); + } +} diff --git a/src/Illuminate/Console/GeneratorCommand.php b/src/Illuminate/Console/GeneratorCommand.php index 5b6af51a576b..a66a5e3b9747 100644 --- a/src/Illuminate/Console/GeneratorCommand.php +++ b/src/Illuminate/Console/GeneratorCommand.php @@ -3,6 +3,7 @@ namespace Illuminate\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; +use Illuminate\Console\Concerns\FindsAvailableModels; use Illuminate\Contracts\Console\PromptsForMissingInput; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Collection; @@ -12,6 +13,8 @@ abstract class GeneratorCommand extends Command implements PromptsForMissingInput { + use FindsAvailableModels; + /** * The filesystem instance. * @@ -239,22 +242,6 @@ protected function qualifyModel(string $model) : $rootNamespace.$model; } - /** - * Get a list of possible model names. - * - * @return array - */ - protected function possibleModels() - { - $modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path(); - - return (new Collection(Finder::create()->files()->depth(0)->in($modelPath))) - ->map(fn ($file) => $file->getBasename('.php')) - ->sort() - ->values() - ->all(); - } - /** * Get a list of possible event names. * diff --git a/src/Illuminate/Database/Console/ShowModelCommand.php b/src/Illuminate/Database/Console/ShowModelCommand.php index 3e99153756bf..35eaf69f8250 100644 --- a/src/Illuminate/Database/Console/ShowModelCommand.php +++ b/src/Illuminate/Database/Console/ShowModelCommand.php @@ -2,15 +2,21 @@ namespace Illuminate\Database\Console; +use Illuminate\Console\Concerns\FindsAvailableModels; +use Illuminate\Contracts\Console\PromptsForMissingInput; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Eloquent\ModelInspector; use Illuminate\Support\Collection; use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Output\OutputInterface; +use function Laravel\Prompts\suggest; + #[AsCommand(name: 'model:show')] -class ShowModelCommand extends DatabaseInspectionCommand +class ShowModelCommand extends DatabaseInspectionCommand implements PromptsForMissingInput { + use FindsAvailableModels; + /** * The console command name. * @@ -211,4 +217,16 @@ protected function displayCli($class, $database, $table, $policy, $attributes, $ $this->newLine(); } + + /** + * Prompt for missing input arguments using the returned questions. + * + * @return array + */ + protected function promptForMissingArgumentsUsing(): array + { + return [ + 'model' => fn (): string => suggest('Which model would you like to show?', $this->findAvailableModels()), + ]; + } } diff --git a/src/Illuminate/Foundation/Console/ObserverMakeCommand.php b/src/Illuminate/Foundation/Console/ObserverMakeCommand.php index 2193d8583fd1..65bb38ee4a00 100644 --- a/src/Illuminate/Foundation/Console/ObserverMakeCommand.php +++ b/src/Illuminate/Foundation/Console/ObserverMakeCommand.php @@ -158,8 +158,8 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp } $model = suggest( - 'What model should this observer apply to? (Optional)', - $this->possibleModels(), + 'What model should be observed? (Optional)', + $this->findAvailableModels(), ); if ($model) { diff --git a/src/Illuminate/Foundation/Console/PolicyMakeCommand.php b/src/Illuminate/Foundation/Console/PolicyMakeCommand.php index 521c135b062b..96caa392dcd6 100644 --- a/src/Illuminate/Foundation/Console/PolicyMakeCommand.php +++ b/src/Illuminate/Foundation/Console/PolicyMakeCommand.php @@ -218,7 +218,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp $model = suggest( 'What model should this policy apply to? (Optional)', - $this->possibleModels(), + $this->findAvailableModels(), ); if ($model) { diff --git a/src/Illuminate/Routing/Console/ControllerMakeCommand.php b/src/Illuminate/Routing/Console/ControllerMakeCommand.php index dcf855c3f806..3d6886fbc1f9 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -328,8 +328,8 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp if (in_array($type, ['api', 'resource', 'singleton'])) { $model = suggest( - "What model should this $type controller be for? (Optional)", - $this->possibleModels() + "What model is this $type controller for? (Optional)", + $this->findAvailableModels() ); if ($model) {