@@ -33,63 +33,70 @@ public function buildGraph(Collection $models) : Graph
33
33
34
34
/**
35
35
* Generate a structured text representation of the ER diagram
36
- *
36
+ *
37
37
* @param Collection $models
38
38
* @return string
39
39
*/
40
40
public function generateStructuredTextRepresentation (Collection $ models ) : string
41
41
{
42
42
$ output = "# Entity Relationship Diagram \n\n" ;
43
-
43
+
44
44
// First list all models/entities with their attributes
45
45
$ output .= "## Entities \n\n" ;
46
-
46
+
47
47
foreach ($ models as $ model ) {
48
48
/** @var Model $model */
49
49
$ eloquentModel = app ($ model ->getModel ());
50
50
$ output .= "### " . $ model ->getLabel () . " (` " . $ model ->getModel () . "`) \n\n" ;
51
-
51
+
52
52
// Add table columns if available
53
53
if (config ('erd-generator.use_db_schema ' )) {
54
54
$ columns = $ this ->getTableColumnsFromModel ($ eloquentModel );
55
55
if (count ($ columns ) > 0 ) {
56
56
$ output .= "#### Attributes: \n\n" ;
57
57
foreach ($ columns as $ column ) {
58
- $ columnType = config ('erd-generator.use_column_types ' ) ? ' ( ' . $ column ->getType ()->getName () . ') ' : '' ;
59
- $ output .= "- ` " . $ column ->getName () . "` " . $ columnType . "\n" ;
58
+ if (is_object ($ column )) {
59
+ $ name = $ column ->getName ();
60
+ $ typeName = $ column ->getType ()->getName ();
61
+ } else {
62
+ $ name = $ column ['name ' ] ?? '' ;
63
+ $ typeName = $ column ['type_name ' ] ?? '' ;
64
+ }
65
+ $ columnType = config ('erd-generator.use_column_types ' ) ? ' ( ' . $ typeName . ') ' : '' ;
66
+ $ output .= "- ` " . $ name . "` " . $ columnType . "\n" ;
60
67
}
61
68
$ output .= "\n" ;
62
69
}
63
70
}
64
71
}
65
-
72
+
66
73
// Then list all relationships
67
74
$ output .= "## Relationships \n\n" ;
68
-
75
+
69
76
foreach ($ models as $ model ) {
70
77
/** @var Model $model */
71
78
if (count ($ model ->getRelations ()) > 0 ) {
72
79
$ output .= "### " . $ model ->getLabel () . " Relationships \n\n" ;
73
-
80
+
74
81
foreach ($ model ->getRelations () as $ relation ) {
75
82
/** @var ModelRelation $relation */
76
83
// Find the related model by comparing model class names
77
84
$ relatedModelClass = $ relation ->getModel ();
85
+
78
86
$ relatedModel = $ models ->first (function ($ m ) use ($ relatedModelClass ) {
79
87
return $ m ->getModel () === $ relatedModelClass ;
80
88
});
81
-
82
89
if ($ relatedModel ) {
83
- $ output .= "- ** " . $ relation ->getType () . "** ` " . $ relation ->getName () . "` to " .
84
- $ relatedModel ->getLabel () . " (Local Key: ` " . $ relation ->getLocalKey () .
90
+ $ output .= "- ** " . $ relation ->getType () . "** ` " . $ relation ->getName () . "` to " .
91
+ $ relatedModel ->getLabel () . " (Local Key: ` " . $ relation ->getLocalKey () .
85
92
"`, Foreign Key: ` " . $ relation ->getForeignKey () . "`) \n" ;
86
93
}
87
94
}
88
-
95
+
89
96
$ output .= "\n" ;
90
97
}
91
98
}
92
-
99
+
93
100
return $ output ;
94
101
}
95
102
0 commit comments