From 7faeeaa071cde49448354a2b25efdbd94057b7e3 Mon Sep 17 00:00:00 2001 From: mrazinshaikh Date: Wed, 5 Nov 2025 11:21:47 +0530 Subject: [PATCH 1/4] Refactor GeneratorCommand and ShowModelCommand to use InteractsWithModels trait for model interaction. Moved possibleModels method to the new trait for better code organization. --- .../Console/Concerns/InteractsWithModels.php | 25 +++++++++++++++++++ src/Illuminate/Console/GeneratorCommand.php | 19 +++----------- .../Database/Console/ShowModelCommand.php | 20 ++++++++++++++- 3 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 src/Illuminate/Console/Concerns/InteractsWithModels.php diff --git a/src/Illuminate/Console/Concerns/InteractsWithModels.php b/src/Illuminate/Console/Concerns/InteractsWithModels.php new file mode 100644 index 000000000000..8da518f1f647 --- /dev/null +++ b/src/Illuminate/Console/Concerns/InteractsWithModels.php @@ -0,0 +1,25 @@ + + */ + 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(); + } +} diff --git a/src/Illuminate/Console/GeneratorCommand.php b/src/Illuminate/Console/GeneratorCommand.php index 5b6af51a576b..fb257a53aa32 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\InteractsWithModels; 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 InteractsWithModels; + /** * 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..ad3c6dbcd8ce 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\InteractsWithModels; +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 InteractsWithModels; + /** * 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() => suggest('Which model would you like to show?', $this->possibleModels()), + ]; + } } From aa2ae6456eec8c3aa0c2a364cfda3b9b3fb49fd8 Mon Sep 17 00:00:00 2001 From: mrazinshaikh Date: Wed, 5 Nov 2025 17:37:43 +0530 Subject: [PATCH 2/4] formatting and type improvements --- src/Illuminate/Console/Concerns/InteractsWithModels.php | 2 +- src/Illuminate/Database/Console/ShowModelCommand.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Console/Concerns/InteractsWithModels.php b/src/Illuminate/Console/Concerns/InteractsWithModels.php index 8da518f1f647..e9a3a709454f 100644 --- a/src/Illuminate/Console/Concerns/InteractsWithModels.php +++ b/src/Illuminate/Console/Concerns/InteractsWithModels.php @@ -17,7 +17,7 @@ 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')) + ->map(fn ($file) => $file->getBasename('.php')) ->sort() ->values() ->all(); diff --git a/src/Illuminate/Database/Console/ShowModelCommand.php b/src/Illuminate/Database/Console/ShowModelCommand.php index ad3c6dbcd8ce..8e16ca7cda70 100644 --- a/src/Illuminate/Database/Console/ShowModelCommand.php +++ b/src/Illuminate/Database/Console/ShowModelCommand.php @@ -221,12 +221,12 @@ protected function displayCli($class, $database, $table, $policy, $attributes, $ /** * Prompt for missing input arguments using the returned questions. * - * @return array + * @return array */ protected function promptForMissingArgumentsUsing(): array { return [ - 'model' => fn() => suggest('Which model would you like to show?', $this->possibleModels()), + 'model' => fn (): string => suggest('Which model would you like to show?', $this->possibleModels()), ]; } } From e97bccaf7bf2c63d8208e71953c6fc4f24cf86e8 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 5 Nov 2025 09:26:26 -0600 Subject: [PATCH 3/4] formatting --- .../{InteractsWithModels.php => FindsAvailableModels.php} | 4 ++-- src/Illuminate/Console/GeneratorCommand.php | 4 ++-- src/Illuminate/Database/Console/ShowModelCommand.php | 6 +++--- src/Illuminate/Foundation/Console/ObserverMakeCommand.php | 4 ++-- src/Illuminate/Foundation/Console/PolicyMakeCommand.php | 2 +- src/Illuminate/Routing/Console/ControllerMakeCommand.php | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) rename src/Illuminate/Console/Concerns/{InteractsWithModels.php => FindsAvailableModels.php} (88%) diff --git a/src/Illuminate/Console/Concerns/InteractsWithModels.php b/src/Illuminate/Console/Concerns/FindsAvailableModels.php similarity index 88% rename from src/Illuminate/Console/Concerns/InteractsWithModels.php rename to src/Illuminate/Console/Concerns/FindsAvailableModels.php index e9a3a709454f..e2a81e62b4f0 100644 --- a/src/Illuminate/Console/Concerns/InteractsWithModels.php +++ b/src/Illuminate/Console/Concerns/FindsAvailableModels.php @@ -5,14 +5,14 @@ use Illuminate\Support\Collection; use Symfony\Component\Finder\Finder; -trait InteractsWithModels +trait FindsAvailableModels { /** * Get a list of possible model names. * * @return array */ - protected function possibleModels() + protected function findAvailableModels() { $modelPath = is_dir(app_path('Models')) ? app_path('Models') : app_path(); diff --git a/src/Illuminate/Console/GeneratorCommand.php b/src/Illuminate/Console/GeneratorCommand.php index fb257a53aa32..a66a5e3b9747 100644 --- a/src/Illuminate/Console/GeneratorCommand.php +++ b/src/Illuminate/Console/GeneratorCommand.php @@ -3,7 +3,7 @@ namespace Illuminate\Console; use Illuminate\Console\Concerns\CreatesMatchingTest; -use Illuminate\Console\Concerns\InteractsWithModels; +use Illuminate\Console\Concerns\FindsAvailableModels; use Illuminate\Contracts\Console\PromptsForMissingInput; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Collection; @@ -13,7 +13,7 @@ abstract class GeneratorCommand extends Command implements PromptsForMissingInput { - use InteractsWithModels; + use FindsAvailableModels; /** * The filesystem instance. diff --git a/src/Illuminate/Database/Console/ShowModelCommand.php b/src/Illuminate/Database/Console/ShowModelCommand.php index 8e16ca7cda70..35eaf69f8250 100644 --- a/src/Illuminate/Database/Console/ShowModelCommand.php +++ b/src/Illuminate/Database/Console/ShowModelCommand.php @@ -2,7 +2,7 @@ namespace Illuminate\Database\Console; -use Illuminate\Console\Concerns\InteractsWithModels; +use Illuminate\Console\Concerns\FindsAvailableModels; use Illuminate\Contracts\Console\PromptsForMissingInput; use Illuminate\Contracts\Container\BindingResolutionException; use Illuminate\Database\Eloquent\ModelInspector; @@ -15,7 +15,7 @@ #[AsCommand(name: 'model:show')] class ShowModelCommand extends DatabaseInspectionCommand implements PromptsForMissingInput { - use InteractsWithModels; + use FindsAvailableModels; /** * The console command name. @@ -226,7 +226,7 @@ protected function displayCli($class, $database, $table, $policy, $attributes, $ protected function promptForMissingArgumentsUsing(): array { return [ - 'model' => fn (): string => suggest('Which model would you like to show?', $this->possibleModels()), + '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..ae63ab666b5f 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -329,7 +329,7 @@ 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() + $this->findAvailableModels() ); if ($model) { From 32738cc0db1e6e55b4bbacdd864a10bf1197d6f4 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 5 Nov 2025 09:27:12 -0600 Subject: [PATCH 4/4] formatting --- src/Illuminate/Routing/Console/ControllerMakeCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Routing/Console/ControllerMakeCommand.php b/src/Illuminate/Routing/Console/ControllerMakeCommand.php index ae63ab666b5f..3d6886fbc1f9 100755 --- a/src/Illuminate/Routing/Console/ControllerMakeCommand.php +++ b/src/Illuminate/Routing/Console/ControllerMakeCommand.php @@ -328,7 +328,7 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp if (in_array($type, ['api', 'resource', 'singleton'])) { $model = suggest( - "What model should this $type controller be for? (Optional)", + "What model is this $type controller for? (Optional)", $this->findAvailableModels() );