@@ -84,6 +84,20 @@ private function _getTableRelations()
8484 ];
8585 }
8686
87+ /**
88+ * Extract the table name from a fully qualified table name (e.g., database.table).
89+ * @param string $foreignTable
90+ * @return string
91+ */
92+ protected function extractTableName ($ foreignTable )
93+ {
94+ $ dotPosition = strpos ($ foreignTable , '. ' );
95+ if ($ dotPosition !== false ) {
96+ return substr ($ foreignTable , $ dotPosition + 1 ); // Extract table name only
97+ }
98+ return $ foreignTable ; // No dot found, return the original name
99+ }
100+
87101 protected function getBelongsTo ()
88102 {
89103 $ relations = Schema::getForeignKeys ($ this ->table );
@@ -95,10 +109,12 @@ protected function getBelongsTo()
95109 continue ;
96110 }
97111
112+ $ foreignTable = $ this ->extractTableName ($ table );
113+
98114 $ eloquent [] = [
99115 'name ' => 'belongsTo ' ,
100- 'relation_name ' => Str::camel (Str::singular ($ relation [ ' foreign_table ' ] )),
101- 'class ' => Str::studly (Str::singular ($ relation [ ' foreign_table ' ] )),
116+ 'relation_name ' => Str::camel (Str::singular ($ foreignTable )),
117+ 'class ' => Str::studly (Str::singular ($ foreignTable )),
102118 'foreign_key ' => $ relation ['columns ' ][0 ],
103119 'owner_key ' => $ relation ['foreign_columns ' ][0 ],
104120 ];
@@ -116,6 +132,7 @@ protected function getOtherRelations()
116132 $ relations = Schema::getForeignKeys ($ table );
117133 $ indexes = collect (Schema::getIndexes ($ table ));
118134
135+
119136 foreach ($ relations as $ relation ) {
120137 if ($ relation ['foreign_table ' ] != $ this ->table ) {
121138 continue ;
@@ -126,11 +143,12 @@ protected function getOtherRelations()
126143 }
127144
128145 $ isUniqueColumn = $ this ->getUniqueIndex ($ indexes , $ relation ['columns ' ][0 ]);
146+ $ foreignTable = $ this ->extractTableName ($ table );
129147
130148 $ eloquent [] = [
131149 'name ' => $ isUniqueColumn ? 'hasOne ' : 'hasMany ' ,
132- 'relation_name ' => Str::camel ($ isUniqueColumn ? Str::singular ($ table ) : Str::plural ($ table )),
133- 'class ' => Str::studly (Str::singular ($ table )),
150+ 'relation_name ' => Str::camel ($ isUniqueColumn ? Str::singular ($ foreignTable ) : Str::plural ($ foreignTable )),
151+ 'class ' => Str::studly (Str::singular ($ foreignTable )),
134152 'foreign_key ' => $ relation ['foreign_columns ' ][0 ],
135153 'owner_key ' => $ relation ['columns ' ][0 ],
136154 ];
0 commit comments