@@ -61,11 +61,11 @@ private function queryOrderBuilder($query, array $sortParameters)
61
61
$ explodeResult = SortableLink::explodeSortParameter ($ column );
62
62
if ( ! empty ($ explodeResult )) {
63
63
$ relationName = $ explodeResult [0 ];
64
- $ column = $ explodeResult [1 ];
64
+ $ column = $ explodeResult [1 ];
65
65
66
66
try {
67
67
$ relation = $ query ->getRelation ($ relationName );
68
- $ query = $ this ->queryJoinBuilder ($ query , $ relation );
68
+ $ query = $ this ->queryJoinBuilder ($ query , $ relation );
69
69
} catch (BadMethodCallException $ e ) {
70
70
throw new ColumnSortableException ($ relationName , 1 , $ e );
71
71
} catch (\Exception $ e ) {
@@ -83,7 +83,7 @@ private function queryOrderBuilder($query, array $sortParameters)
83
83
$ query = $ query ->orderBy ($ column , $ direction );
84
84
} elseif ($ this ->columnExists ($ model , $ column )) {
85
85
$ column = $ model ->getTable ().'. ' .$ column ;
86
- $ query = $ query ->orderBy ($ column , $ direction );
86
+ $ query = $ query ->orderBy ($ column , $ direction );
87
87
}
88
88
89
89
return $ query ;
@@ -122,16 +122,22 @@ private function parseSortParameters(array $sortParameters)
122
122
private function queryJoinBuilder ($ query , $ relation )
123
123
{
124
124
$ relatedTable = $ relation ->getRelated ()->getTable ();
125
- $ parentTable = $ relation ->getParent ()->getTable ();
125
+ $ parentTable = $ relation ->getParent ()->getTable ();
126
+
127
+ if ($ parentTable === $ relatedTable ) {
128
+ $ query = $ query ->from ($ parentTable .' as parent_ ' .$ parentTable );
129
+ $ parentTable = 'parent_ ' .$ parentTable ;
130
+ $ relation ->getParent ()->setTable ($ parentTable );
131
+ }
126
132
127
133
if ($ relation instanceof HasOne) {
128
134
$ relatedPrimaryKey = $ relation ->getQualifiedForeignKeyName ();
129
- $ parentPrimaryKey = $ relation ->getQualifiedParentKeyName ();
135
+ $ parentPrimaryKey = $ relation ->getQualifiedParentKeyName ();
130
136
131
137
return $ query ->select ($ parentTable .'.* ' )->join ($ relatedTable , $ parentPrimaryKey , '= ' , $ relatedPrimaryKey );
132
138
} elseif ($ relation instanceof BelongsTo) {
133
139
$ relatedPrimaryKey = $ relation ->getQualifiedOwnerKeyName ();
134
- $ parentPrimaryKey = $ relation ->getQualifiedForeignKey ();
140
+ $ parentPrimaryKey = $ relation ->getQualifiedForeignKey ();
135
141
136
142
return $ query ->select ($ parentTable .'.* ' )->join ($ relatedTable , $ parentPrimaryKey , '= ' , $ relatedPrimaryKey );
137
143
} else {
@@ -148,8 +154,8 @@ private function queryJoinBuilder($query, $relation)
148
154
*/
149
155
private function columnExists ($ model , $ column )
150
156
{
151
- return (isset ($ model ->sortable )) ? in_array ($ column , $ model ->sortable ) : Schema:: hasColumn ( $ model -> getTable (),
152
- $ column );
157
+ return (isset ($ model ->sortable )) ? in_array ($ column , $ model ->sortable ) :
158
+ Schema:: hasColumn ( $ model -> getTable (), $ column );
153
159
}
154
160
155
161
@@ -175,7 +181,7 @@ private function formatToSortParameters($sort)
175
181
176
182
return ($ each [0 ] === 0 ) ? ['sort ' => $ each [1 ], 'order ' => $ configDefaultOrder ] : [
177
183
'sort ' => $ each [0 ],
178
- 'order ' => $ each [1 ]
184
+ 'order ' => $ each [1 ],
179
185
];
180
186
}
181
187
}
0 commit comments