From 6c0c80f8be1a65da95014526aa2c717b51f02f42 Mon Sep 17 00:00:00 2001 From: Michele Capicchioni Date: Thu, 11 Jul 2024 23:27:41 +0200 Subject: [PATCH] Fix SchemaFactoryMakeCommand.php for laravel 11 Fix this code to work with laravel 11 --- src/Console/SchemaFactoryMakeCommand.php | 25 +++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Console/SchemaFactoryMakeCommand.php b/src/Console/SchemaFactoryMakeCommand.php index 3a4b817..dd9e501 100644 --- a/src/Console/SchemaFactoryMakeCommand.php +++ b/src/Console/SchemaFactoryMakeCommand.php @@ -48,19 +48,26 @@ protected function buildModel($output, $model) $columns = SchemaFacade::connection($model->getConnectionName())->getColumnListing(config('database.connections.'.config('database.default').'.prefix', '').$model->getTable()); $connection = $model->getConnection(); - + + $tableName = config('database.connections.'.config('database.default').'.prefix', '').$model->getTable(); + + $definition = 'return Schema::object(\''.class_basename($model).'\')'.PHP_EOL; $definition .= ' ->properties('.PHP_EOL; $properties = collect($columns) - ->map(static function ($column) use ($model, $connection) { - /** @var Column $column */ - $column = $connection->getDoctrineColumn(config('database.connections.'.config('database.default').'.prefix', '').$model->getTable(), $column); - $name = $column->getName(); - $default = $column->getDefault(); - $notNull = $column->getNotnull(); - - switch (get_class($column->getType())) { + ->map(static function ($columnName) use ($model, $connection, $tableName) { +// /** @var Column $column */ +// $column = $connection->getDoctrineColumn(config('database.connections.'.config('database.default').'.prefix', '').$model->getTable(), $column); + $columnType = $connection->getSchemaBuilder()->getColumnType( + $tableName, + $columnName + ); + $name = $columnName; + $default = $connection->select("SELECT COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$tableName' AND COLUMN_NAME = '$columnName'")[0]->COLUMN_DEFAULT; + $notNull = $connection->select("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$tableName' AND COLUMN_NAME = '$columnName'")[0]->IS_NULLABLE === 'NO'; + + switch ($columnType) { case IntegerType::class: $format = 'Schema::integer(%s)->default(%s)'; $args = [$name, $notNull ? (int) $default : null];