Skip to content

Commit

Permalink
fix: Update SchemaFactoryMakeCommand to improve new code readability
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
capimichi committed Jul 13, 2024
1 parent 63233ce commit 93f9aa8
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/Console/SchemaFactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down

0 comments on commit 93f9aa8

Please sign in to comment.