Skip to content

Commit 1572c0e

Browse files
authored
Merge pull request #118 from mrunkel/master
Use an alternate method of reading column details if the old way fails
2 parents f05eb01 + 9818964 commit 1572c0e

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/GraphBuilder.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace BeyondCode\ErdGenerator;
44

55
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
6+
use Illuminate\Support\Facades\Schema;
67
use phpDocumentor\GraphViz\Graph;
78
use Illuminate\Support\Collection;
89
use phpDocumentor\GraphViz\Node;
@@ -111,6 +112,11 @@ protected function getTableColumnsFromModel(EloquentModel $model)
111112
} catch (\Throwable $e) {
112113
}
113114

115+
try {
116+
return Schema::getColumns($model->getTable());
117+
} catch (\Throwable $e) {
118+
}
119+
114120
return [];
115121
}
116122

@@ -123,11 +129,18 @@ protected function getModelLabel(EloquentModel $model, string $label)
123129
if (config('erd-generator.use_db_schema')) {
124130
$columns = $this->getTableColumnsFromModel($model);
125131
foreach ($columns as $column) {
126-
$label = $column->getName();
132+
if (is_object($column)) {
133+
$name = $column->getName();
134+
$typeName = $column->getType()->getName();
135+
} else { // it's an array!
136+
$name = $column['name'] ?? '';
137+
$typeName = $column['type_name'] ?? '';
138+
}
139+
$label = $name;
127140
if (config('erd-generator.use_column_types')) {
128-
$label .= ' ('.$column->getType()->getName().')';
141+
$label .= ' ('. $typeName .')';
129142
}
130-
$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;
143+
$table .= '<tr width="100%"><td port="' . $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;
131144
}
132145
}
133146

0 commit comments

Comments
 (0)