digitaldreams / laracrud

Laravel Code Generator based on MySQL Database
MIT License
331 stars 54 forks source link

Error during the migration generation #29

Open lavapi opened 4 years ago

lavapi commented 4 years ago

I am getting an error when trying to generate migration from DB.

vendor\digitaldream\laracrud\src\lara-crud\Crud\MigrationCrud.php:222 218| @throws \Exception 219| / 220| public function generateClassName($table) 221| {

222| $class = 'create' . ucfirst(camel_case($table)) . 'Table'; 223| 224| if (class_exists($class)) { 225| throw new \Exception('Migration for table ' . $table . ' already exists'); 226| }

Exception trace:

1 LaraCrud\Crud\MigrationCrud::generateClassName("users") vendor\digitaldream\laracrud\src\lara-crud\Crud\MigrationCrud.php:94

2 LaraCrud\Crud\MigrationCrud::template() vendor\digitaldream\laracrud\src\lara-crud\Crud\MigrationCrud.php:110

Please use the argument -v to see more details.

lavapi commented 4 years ago

Can you just replace camel_case($table) with Str::camel($table)

digitaldreams commented 4 years ago

Thanks for letting me know. Yes. i will definitely do that. I am working with V5 and its really cool. Until you have to wait.

digitaldreams commented 4 years ago

I hope you got the error. You already have a migration for your users table. Try something else and it should create migration methods for you.

lavapi commented 4 years ago

I fixed it temporarily but just informed you to fix it permanently :) P.S. perfect work...

hardkraft commented 4 years ago

Hi, in addition to this error I discovered that you don't handle the UNSIGNED attribute in MySQL 8. The "unsigned" is returned with the column data type so it needs to be split first, otherwise the rules method won't match anything and returns nothing for the methodName which causes errors on line 71. Also, it seems to me that you only consider int and bigint for primary keys. Is there a reason for that?

hardkraft commented 4 years ago

I made some changes starting on line 127

` $dataTypes = explode(' ',$column->type()); $dataType = $dataTypes[0]; $dataTypeAttr = !empty($dataTypes[1])? $dataTypes[1]:'';

        if ($column->isPk()) {
            if ($dataType == 'int') {
                $arr['methodName'] = 'increments';
            } elseif ($dataType == 'bigint') {
                $arr['methodName'] = 'bigIncrements';
            }
        } 
        if (empty($arr['methodName'])) {
            $arr['methodName'] = isset($this->columnMap[$dataType]) ? $this->columnMap[$dataType] : '';
        }`

I also added 'json' => 'json', to your $columnMap

Works well for my purposes but I didn't test it on anything else. Thanks for a great project :)

digitaldreams commented 4 years ago

@hardkraft thanks for code suggestion. I will definitely implement on next major version 5.0 which is under development now.

nicholasbieber commented 3 years ago

I received this exact issue today in a fresh project, fresh "composer require digitaldream/laracrud --dev" - and line 222 had: $class = 'create' . ucfirst(camel_case($table)) . 'Table'; instead of $class = 'create'.Str::camel($table).'Table'; I swapped the lines out and it's fine now... I wish I could offer some insight but I'm a noob out of their depth. Probably something on this end. Otherwise it all looks very spiffy!

digitaldreams commented 3 years ago

Hi @nicholasbieber This issue is fixed on dev-master. You can use this version. The next Major version 5 is expected to be release in October this year.