Skip to content

Commit

Permalink
FIX added backtick in sql query to get the table columns. Added the c…
Browse files Browse the repository at this point in the history
…reation of directories recursively if it does not find the livewire dir required. Added a fix for this to work on any laravel 9 version.
  • Loading branch information
Marcos Sigueros committed Feb 4, 2023
1 parent f1e45ea commit c714c36
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ composer.lock
docs
vendor
coverage
node_modules
node_modules
.idea
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
],
"require": {
"php": "^7|^8",
"laravel/framework": "^6|^7|^8|<9.19",
"laravel/framework": "^6|^7|^8|^9",
"laravel/ui": "^2|^3|^4",
"livewire/livewire": "^2"
},
Expand Down
25 changes: 13 additions & 12 deletions src/Commands/LivewireGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ abstract class LivewireGeneratorCommand extends Command
* Controller Namespace.
* @var string
*/
protected $controllerNamespace = 'App\Http\Controllers';
protected $controllerNamespace = 'App\Http\Controllers';
/**
* Controller Namespace.
* @var string
Expand Down Expand Up @@ -133,6 +133,7 @@ protected function makeDirectory($path)
*/
protected function write($path, $content)
{
$this->files->makeDirectory(dirname($path), 0755, true, true);
$this->files->put($path, $content);
}

Expand Down Expand Up @@ -181,11 +182,11 @@ private function _getSpace($no = 1)
protected function _getMigrationPath($name)
{
return base_path("database/migrations/". date('Y-m-d_His') ."_create_". Str::lower(Str::plural($name)) ."_table.php");
}
}
protected function _getFactoryPath($name)
{
return base_path("database/factories/{$name}Factory.php");
}
}

/**
* @param $name
Expand Down Expand Up @@ -337,7 +338,7 @@ protected function buildLayout(): void
protected function getColumns()
{
if (empty($this->tableColumns)) {
$this->tableColumns = DB::select('SHOW COLUMNS FROM ' . $this->table);
$this->tableColumns = DB::select('SHOW COLUMNS FROM `' . $this->table.'`');
}

return $this->tableColumns;
Expand All @@ -358,7 +359,7 @@ protected function getFilteredColumns()
return array_filter($columns, function ($value) use ($unwanted) {
return !in_array($value, $unwanted);
});
}
}

/**
* Make model attributes/replacements.
Expand Down Expand Up @@ -421,7 +422,7 @@ protected function modelReplacements()

// CSV format
return implode(', ', $filterColumns);
};
};

$resetfields = function () {

Expand All @@ -436,8 +437,8 @@ protected function modelReplacements()

// CSV format
return implode('', $filterColumns);
};
};

$addfields = function () {

/** @var array $filterColumns Exclude the unwanted columns */
Expand All @@ -450,8 +451,8 @@ protected function modelReplacements()

// CSV format
return implode(',', $filterColumns);
};
};

$keyWord = function () {

/** @var array $filterColumns Exclude the unwanted columns */
Expand All @@ -464,7 +465,7 @@ protected function modelReplacements()

// CSV format
return implode('', $filterColumns);
};
};

$factoryfields = function () {

Expand All @@ -479,7 +480,7 @@ protected function modelReplacements()
// CSV format
return implode('', $filterColumns);
};

$editfields = function () {

/** @var array $filterColumns Exclude the unwanted columns */
Expand Down
12 changes: 6 additions & 6 deletions src/ModelGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ private function _getTableRelations()
{
$db = DB::getDatabaseName();
$sql = <<<SQL
SELECT TABLE_NAME ref_table, COLUMN_NAME foreign_key, REFERENCED_COLUMN_NAME local_key, '1' ref
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
SELECT TABLE_NAME ref_table, COLUMN_NAME foreign_key, REFERENCED_COLUMN_NAME local_key, '1' ref
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_NAME = '$this->table' AND TABLE_SCHEMA = '$db'
UNION
SELECT REFERENCED_TABLE_NAME ref_table, REFERENCED_COLUMN_NAME foreign_key, COLUMN_NAME local_key, '0' ref
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = '$this->table' AND TABLE_SCHEMA = '$db' AND REFERENCED_TABLE_NAME IS NOT NULL
SELECT REFERENCED_TABLE_NAME ref_table, REFERENCED_COLUMN_NAME foreign_key, COLUMN_NAME local_key, '0' ref
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME = '$this->table' AND TABLE_SCHEMA = '$db' AND REFERENCED_TABLE_NAME IS NOT NULL
ORDER BY ref_table ASC
SQL;
Expand All @@ -135,6 +135,6 @@ private function _getTableRelations()
*/
private function _getTableKeys($table)
{
return DB::select("SHOW KEYS FROM {$table}");
return DB::select("SHOW KEYS FROM `{$table}`");
}
}

0 comments on commit c714c36

Please sign in to comment.