@@ -39,7 +39,6 @@ public function scopeSortable($query, $defaultSortParameters = null)
3939 }
4040 }
4141
42-
4342 /**
4443 * @param \Illuminate\Database\Query\Builder $query
4544 * @param array $sortParameters
@@ -51,18 +50,14 @@ public function scopeSortable($query, $defaultSortParameters = null)
5150 private function queryOrderBuilder ($ query , array $ sortParameters )
5251 {
5352 $ model = $ this ;
54-
5553 list ($ column , $ direction ) = $ this ->parseSortParameters ($ sortParameters );
56-
5754 if (is_null ($ column )) {
5855 return $ query ;
5956 }
60-
6157 $ explodeResult = SortableLink::explodeSortParameter ($ column );
6258 if ( ! empty ($ explodeResult )) {
6359 $ relationName = $ explodeResult [0 ];
6460 $ column = $ explodeResult [1 ];
65-
6661 try {
6762 $ relation = $ query ->getRelation ($ relationName );
6863 $ query = $ this ->queryJoinBuilder ($ query , $ relation );
@@ -71,25 +66,19 @@ private function queryOrderBuilder($query, array $sortParameters)
7166 } catch (\Exception $ e ) {
7267 throw new ColumnSortableException ($ relationName , 2 , $ e );
7368 }
74-
7569 $ model = $ relation ->getRelated ();
7670 }
77-
7871 if (method_exists ($ model , camel_case ($ column ).'Sortable ' )) {
7972 return call_user_func_array ([$ model , camel_case ($ column ).'Sortable ' ], [$ query , $ direction ]);
8073 }
81-
8274 if (isset ($ model ->sortableAs ) && in_array ($ column , $ model ->sortableAs )) {
8375 $ query = $ query ->orderBy ($ column , $ direction );
8476 } elseif ($ this ->columnExists ($ model , $ column )) {
8577 $ column = $ model ->getTable ().'. ' .$ column ;
8678 $ query = $ query ->orderBy ($ column , $ direction );
8779 }
88-
8980 return $ query ;
9081 }
91-
92-
9382 /**
9483 * @param array $sortParameters
9584 *
@@ -101,16 +90,12 @@ private function parseSortParameters(array $sortParameters)
10190 if (empty ($ column )) {
10291 return [null , null ];
10392 }
104-
10593 $ direction = array_get ($ sortParameters , 'order ' , []);
10694 if ( ! in_array ($ direction , ['asc ' , 'desc ' ])) {
10795 $ direction = Config::get ('columnsortable.default_direction ' , 'asc ' );
10896 }
109-
11097 return [$ column , $ direction ];
11198 }
112-
113-
11499 /**
115100 * @param \Illuminate\Database\Query\Builder $query
116101 * @param $relation
@@ -123,29 +108,23 @@ private function queryJoinBuilder($query, $relation)
123108 {
124109 $ relatedTable = $ relation ->getRelated ()->getTable ();
125110 $ parentTable = $ relation ->getParent ()->getTable ();
126-
127111 if ($ parentTable === $ relatedTable ) {
128112 $ query = $ query ->from ($ parentTable .' as parent_ ' .$ parentTable );
129113 $ parentTable = 'parent_ ' .$ parentTable ;
130114 $ relation ->getParent ()->setTable ($ parentTable );
131115 }
132-
133116 if ($ relation instanceof HasOne) {
134117 $ relatedPrimaryKey = $ relation ->getQualifiedForeignKeyName ();
135118 $ parentPrimaryKey = $ relation ->getQualifiedParentKeyName ();
136-
137- return $ query ->select ($ parentTable .'.* ' )->join ($ relatedTable , $ parentPrimaryKey , '= ' , $ relatedPrimaryKey );
119+ return $ this ->formJoin ($ query , $ parentTable , $ relatedTable , $ parentPrimaryKey , $ relatedPrimaryKey );
138120 } elseif ($ relation instanceof BelongsTo) {
139121 $ relatedPrimaryKey = $ relation ->getQualifiedOwnerKeyName ();
140122 $ parentPrimaryKey = $ relation ->getQualifiedForeignKey ();
141-
142- return $ query ->select ($ parentTable .'.* ' )->join ($ relatedTable , $ parentPrimaryKey , '= ' , $ relatedPrimaryKey );
123+ return $ this ->formJoin ($ query , $ parentTable , $ relatedTable , $ parentPrimaryKey , $ relatedPrimaryKey );
143124 } else {
144125 throw new \Exception ();
145126 }
146127 }
147-
148-
149128 /**
150129 * @param $model
151130 * @param $column
@@ -157,8 +136,6 @@ private function columnExists($model, $column)
157136 return (isset ($ model ->sortable )) ? in_array ($ column , $ model ->sortable ) :
158137 Schema::hasColumn ($ model ->getTable (), $ column );
159138 }
160-
161-
162139 /**
163140 * @param array|string $sort
164141 *
@@ -169,19 +146,30 @@ private function formatToSortParameters($sort)
169146 if (empty ($ sort )) {
170147 return [];
171148 }
172-
173149 $ configDefaultOrder = Config::get ('columnsortable.default_direction ' , 'asc ' );
174-
175150 if (is_string ($ sort )) {
176151 return ['sort ' => $ sort , 'order ' => $ configDefaultOrder ];
177152 }
178-
179153 reset ($ sort );
180154 $ each = each ($ sort );
181-
182155 return ($ each [0 ] === 0 ) ? ['sort ' => $ each [1 ], 'order ' => $ configDefaultOrder ] : [
183156 'sort ' => $ each [0 ],
184157 'order ' => $ each [1 ],
185158 ];
186159 }
187- }
160+ /**
161+ * @param $query
162+ * @param $parentTable
163+ * @param $relatedTable
164+ * @param $parentPrimaryKey
165+ * @param $relatedPrimaryKey
166+ *
167+ * @return mixed
168+ */
169+ private function formJoin ($ query , $ parentTable , $ relatedTable , $ parentPrimaryKey , $ relatedPrimaryKey )
170+ {
171+ $ joinType = Config::get ('columnsortable.join_type ' , 'join ' );
172+
173+ return $ query ->select ($ parentTable .'.* ' )->{$ joinType }($ relatedTable , $ parentPrimaryKey , '= ' , $ relatedPrimaryKey );
174+ }
175+ }
0 commit comments