@@ -190,32 +190,33 @@ protected function makeSureColumnHasEntity($column)
190190 if (strpos ($ column ['name ' ], '. ' ) !== false ) {
191191 $ possibleMethodName = Str::before ($ column ['name ' ], '. ' );
192192
193- // if the first part of the string exists as method,
194- // it is a relationship
193+ // if the first part of the string exists as method in the model
195194 if (method_exists ($ this ->model , $ possibleMethodName )) {
196195
197- // if it has parameters it's not a relation method.
198- $ column ['entity ' ] = $ this ->modelMethodHasParameters ($ this ->model , $ possibleMethodName ) ? false : $ column ['name ' ];
196+ // check model method for possibility of being a relationship
197+ $ column ['entity ' ] = $ this ->modelMethodIsRelationship ($ this ->model , $ possibleMethodName ) ? $ column ['name ' ] : false ;
199198
200- $ parts = explode ('. ' , $ column ['entity ' ]);
199+ if ($ column ['entity ' ]) {
200+ $ parts = explode ('. ' , $ column ['entity ' ]);
201201
202- $ attribute_in_relation = false ;
202+ $ attribute_in_relation = false ;
203203
204- $ model = $ this ->model ;
204+ $ model = $ this ->model ;
205205
206- // here we are going to iterate through all relation parts to check
207- // if the attribute is present in the relation string.
208- foreach ($ parts as $ i => $ part ) {
209- try {
210- $ model = $ model ->$ part ()->getRelated ();
211- } catch (\Exception $ e ) {
212- $ attribute_in_relation = true ;
206+ // here we are going to iterate through all relation parts to check
207+ // if the attribute is present in the relation string.
208+ foreach ($ parts as $ i => $ part ) {
209+ try {
210+ $ model = $ model ->$ part ()->getRelated ();
211+ } catch (\Exception $ e ) {
212+ $ attribute_in_relation = true ;
213+ }
214+ }
215+ // if the user setup the attribute in relation string, we are not going to infer that attribute from model
216+ // instead we get the defined attribute by the user.
217+ if ($ attribute_in_relation ) {
218+ $ column ['attribute ' ] = $ column ['attribute ' ] ?? end ($ parts );
213219 }
214- }
215- // if the user setup the attribute in relation string, we are not going to infer that attribute from model
216- // instead we get the defined attribute by the user.
217- if ($ attribute_in_relation ) {
218- $ column ['attribute ' ] = $ column ['attribute ' ] ?? end ($ parts );
219220 }
220221
221222 return $ column ;
@@ -225,8 +226,8 @@ protected function makeSureColumnHasEntity($column)
225226 // if there's a method on the model with this name
226227 if (method_exists ($ this ->model , $ column ['name ' ])) {
227228
228- // if it has parameters it's not a relation method.
229- $ column ['entity ' ] = $ this ->modelMethodHasParameters ($ this ->model , $ column ['name ' ]) ? false : $ column [ ' name ' ] ;
229+ // check model method for possibility of being a relationship
230+ $ column ['entity ' ] = $ this ->modelMethodIsRelationship ($ this ->model , $ column ['name ' ]);
230231
231232 return $ column ;
232233 }
@@ -237,9 +238,8 @@ protected function makeSureColumnHasEntity($column)
237238 $ possibleMethodName = Str::replaceLast ('_id ' , '' , $ column ['name ' ]);
238239
239240 if (method_exists ($ this ->model , $ possibleMethodName )) {
240-
241- // if it has parameters it's not a relation method.
242- $ column ['entity ' ] = $ this ->modelMethodHasParameters ($ this ->model , $ possibleMethodName ) ? false : $ possibleMethodName ;
241+ // check model method for possibility of being a relationship
242+ $ column ['entity ' ] = $ this ->modelMethodIsRelationship ($ this ->model , $ possibleMethodName );
243243
244244 return $ column ;
245245 }
0 commit comments