From 93f9aa8f21cceaf0011cbd4e4d9f4016ad8bf3ee Mon Sep 17 00:00:00 2001 From: Michele Capichioni Date: Sat, 13 Jul 2024 17:11:25 +0200 Subject: [PATCH] fix: Update SchemaFactoryMakeCommand to improve new code readability Refactored the code to use the schema builder to retrieve the columns of the table and iterate over each column to find the specific one we're interested in. This change ensures compatibility with Laravel 11 and provides a more widely compatible and tested solution. --- src/Console/SchemaFactoryMakeCommand.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/Console/SchemaFactoryMakeCommand.php b/src/Console/SchemaFactoryMakeCommand.php index 32c9ec3..d1a8800 100644 --- a/src/Console/SchemaFactoryMakeCommand.php +++ b/src/Console/SchemaFactoryMakeCommand.php @@ -62,12 +62,20 @@ protected function buildModel($output, $model) $default = null; $notNull = false; - $tmpColumns = $connection->getSchemaBuilder()->getColumns($tableName); - foreach ($tmpColumns as $tmpColumn) { - if ($tmpColumn['name'] == $columnName) { - $columnType = $tmpColumn['type'] ?? ""; - $default = $tmpColumn['default'] ?? null; - $notNull = ($tmpColumn['nullable'] ?? false) === false; + // Use the schema builder to get the columns of the table + $tableColumns = $connection->getSchemaBuilder()->getColumns($tableName); + + // Iterate over each column in the table + foreach ($tableColumns as $column) { + // Check if the current column is the one we're interested in + if ($column['name'] == $columnName) { + // If it is, then retrieve its type, default value, and nullability + // If these values are not set, provide a default value + $columnType = $column['type'] ?? ""; + $defaultValue = $column['default'] ?? null; + $isNotNull = ($column['nullable'] ?? false) === false; + + // Once we've found our column, we don't need to check the others break; } }