Skip to content

Commit 62ebdc4

Browse files
committed
Use an alternate method of reading column details if the old way fails
1 parent 07254be commit 62ebdc4

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;
@@ -49,6 +50,11 @@ protected function getTableColumnsFromModel(EloquentModel $model)
4950
} catch (\Throwable $e) {
5051
}
5152

53+
try {
54+
return Schema::getColumns($model->getTable());
55+
} catch (\Throwable $e) {
56+
}
57+
5258
return [];
5359
}
5460

@@ -61,11 +67,18 @@ protected function getModelLabel(EloquentModel $model, string $label)
6167
if (config('erd-generator.use_db_schema')) {
6268
$columns = $this->getTableColumnsFromModel($model);
6369
foreach ($columns as $column) {
64-
$label = $column->getName();
70+
if (is_object($column)) {
71+
$name = $column->getName();
72+
$typeName = $column->getType()->getName();
73+
} else { // it's an array!
74+
$name = $column['name'];
75+
$typeName = $column['type_name'];
76+
}
77+
$label = $name;
6578
if (config('erd-generator.use_column_types')) {
66-
$label .= ' ('.$column->getType()->getName().')';
79+
$label .= ' ('. $typeName .')';
6780
}
68-
$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;
81+
$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;
6982
}
7083
}
7184

0 commit comments

Comments
 (0)