Skip to content

Use laravel getColumns for getting columns names and types. Add ignore_columns option #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@
*/
'use_column_types' => true,

/*
* If you want to ignore specific columns in specific tables you can specify them here.
* Use the dot notation to specify the table and the column : table.column
* This option only apply when 'use_db_schema' is set to true.
*/
'ignore_columns' => [
// 'users.lastname',
// 'posts.description',
],

/*
* These colors will be used in the table representation for each entity in
* your graph.
Expand Down
34 changes: 20 additions & 14 deletions src/GraphBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Collection;
use phpDocumentor\GraphViz\Node;
use \Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Support\Facades\Schema;

class GraphBuilder
{
Expand Down Expand Up @@ -34,18 +35,19 @@ protected function getTableColumnsFromModel(EloquentModel $model)
{
try {

$table = $model->getConnection()->getTablePrefix() . $model->getTable();
$schema = $model->getConnection()->getDoctrineSchemaManager($table);
$databasePlatform = $schema->getDatabasePlatform();
$databasePlatform->registerDoctrineTypeMapping('enum', 'string');

$database = null;

if (strpos($table, '.')) {
list($database, $table) = explode('.', $table);
$table = $model->getTable();
$columns = Schema::getColumns($table);
if (config('erd-generator.ignore_columns')) {
$columns = collect($columns)->filter(function($column) use($table) {
if (isset($column['name'])){
return !in_array($table.'.'.$column['name'],config('erd-generator.ignore_columns'));
}
return false;
});
}

return $schema->listTableColumns($table, $database);
return $columns;

} catch (\Throwable $e) {
}

Expand All @@ -61,11 +63,15 @@ protected function getModelLabel(EloquentModel $model, string $label)
if (config('erd-generator.use_db_schema')) {
$columns = $this->getTableColumnsFromModel($model);
foreach ($columns as $column) {
$label = $column->getName();
if (config('erd-generator.use_column_types')) {
$label .= ' ('.$column->getType()->getName().')';

if (isset($column['name'])) {
$label = $column['name'];
if (config('erd-generator.use_column_types') && isset($column['type'])) {
$label .= ' ('.$column['type'].')';
}
$table .= '<tr width="100%"><td port="' . $column['name'] . '" align="left" width="100%" bgcolor="'.config('erd-generator.table.row_background_color').'"><font color="'.config('erd-generator.table.row_font_color').'" >' . $label . '</font></td></tr>' . PHP_EOL;
}
$table .= '<tr width="100%"><td port="' . $column->getName() . '" align="left" width="100%" bgcolor="'.config('erd-generator.table.row_background_color').'"><font color="'.config('erd-generator.table.row_font_color').'" >' . $label . '</font></td></tr>' . PHP_EOL;

}
}

Expand Down