@@ -39,7 +39,6 @@ public function scopeSortable($query, $defaultSortParameters = null)
39
39
}
40
40
}
41
41
42
-
43
42
/**
44
43
* @param \Illuminate\Database\Query\Builder $query
45
44
* @param array $sortParameters
@@ -51,18 +50,14 @@ public function scopeSortable($query, $defaultSortParameters = null)
51
50
private function queryOrderBuilder ($ query , array $ sortParameters )
52
51
{
53
52
$ model = $ this ;
54
-
55
53
list ($ column , $ direction ) = $ this ->parseSortParameters ($ sortParameters );
56
-
57
54
if (is_null ($ column )) {
58
55
return $ query ;
59
56
}
60
-
61
57
$ explodeResult = SortableLink::explodeSortParameter ($ column );
62
58
if ( ! empty ($ explodeResult )) {
63
59
$ relationName = $ explodeResult [0 ];
64
60
$ column = $ explodeResult [1 ];
65
-
66
61
try {
67
62
$ relation = $ query ->getRelation ($ relationName );
68
63
$ query = $ this ->queryJoinBuilder ($ query , $ relation );
@@ -71,25 +66,19 @@ private function queryOrderBuilder($query, array $sortParameters)
71
66
} catch (\Exception $ e ) {
72
67
throw new ColumnSortableException ($ relationName , 2 , $ e );
73
68
}
74
-
75
69
$ model = $ relation ->getRelated ();
76
70
}
77
-
78
71
if (method_exists ($ model , camel_case ($ column ).'Sortable ' )) {
79
72
return call_user_func_array ([$ model , camel_case ($ column ).'Sortable ' ], [$ query , $ direction ]);
80
73
}
81
-
82
74
if (isset ($ model ->sortableAs ) && in_array ($ column , $ model ->sortableAs )) {
83
75
$ query = $ query ->orderBy ($ column , $ direction );
84
76
} elseif ($ this ->columnExists ($ model , $ column )) {
85
77
$ column = $ model ->getTable ().'. ' .$ column ;
86
78
$ query = $ query ->orderBy ($ column , $ direction );
87
79
}
88
-
89
80
return $ query ;
90
81
}
91
-
92
-
93
82
/**
94
83
* @param array $sortParameters
95
84
*
@@ -101,16 +90,12 @@ private function parseSortParameters(array $sortParameters)
101
90
if (empty ($ column )) {
102
91
return [null , null ];
103
92
}
104
-
105
93
$ direction = array_get ($ sortParameters , 'order ' , []);
106
94
if ( ! in_array ($ direction , ['asc ' , 'desc ' ])) {
107
95
$ direction = Config::get ('columnsortable.default_direction ' , 'asc ' );
108
96
}
109
-
110
97
return [$ column , $ direction ];
111
98
}
112
-
113
-
114
99
/**
115
100
* @param \Illuminate\Database\Query\Builder $query
116
101
* @param $relation
@@ -123,29 +108,23 @@ private function queryJoinBuilder($query, $relation)
123
108
{
124
109
$ relatedTable = $ relation ->getRelated ()->getTable ();
125
110
$ parentTable = $ relation ->getParent ()->getTable ();
126
-
127
111
if ($ parentTable === $ relatedTable ) {
128
112
$ query = $ query ->from ($ parentTable .' as parent_ ' .$ parentTable );
129
113
$ parentTable = 'parent_ ' .$ parentTable ;
130
114
$ relation ->getParent ()->setTable ($ parentTable );
131
115
}
132
-
133
116
if ($ relation instanceof HasOne) {
134
117
$ relatedPrimaryKey = $ relation ->getQualifiedForeignKeyName ();
135
118
$ parentPrimaryKey = $ relation ->getQualifiedParentKeyName ();
136
-
137
- return $ query ->select ($ parentTable .'.* ' )->join ($ relatedTable , $ parentPrimaryKey , '= ' , $ relatedPrimaryKey );
119
+ return $ this ->formJoin ($ query , $ parentTable , $ relatedTable , $ parentPrimaryKey , $ relatedPrimaryKey );
138
120
} elseif ($ relation instanceof BelongsTo) {
139
121
$ relatedPrimaryKey = $ relation ->getQualifiedOwnerKeyName ();
140
122
$ parentPrimaryKey = $ relation ->getQualifiedForeignKey ();
141
-
142
- return $ query ->select ($ parentTable .'.* ' )->join ($ relatedTable , $ parentPrimaryKey , '= ' , $ relatedPrimaryKey );
123
+ return $ this ->formJoin ($ query , $ parentTable , $ relatedTable , $ parentPrimaryKey , $ relatedPrimaryKey );
143
124
} else {
144
125
throw new \Exception ();
145
126
}
146
127
}
147
-
148
-
149
128
/**
150
129
* @param $model
151
130
* @param $column
@@ -157,8 +136,6 @@ private function columnExists($model, $column)
157
136
return (isset ($ model ->sortable )) ? in_array ($ column , $ model ->sortable ) :
158
137
Schema::hasColumn ($ model ->getTable (), $ column );
159
138
}
160
-
161
-
162
139
/**
163
140
* @param array|string $sort
164
141
*
@@ -169,19 +146,30 @@ private function formatToSortParameters($sort)
169
146
if (empty ($ sort )) {
170
147
return [];
171
148
}
172
-
173
149
$ configDefaultOrder = Config::get ('columnsortable.default_direction ' , 'asc ' );
174
-
175
150
if (is_string ($ sort )) {
176
151
return ['sort ' => $ sort , 'order ' => $ configDefaultOrder ];
177
152
}
178
-
179
153
reset ($ sort );
180
154
$ each = each ($ sort );
181
-
182
155
return ($ each [0 ] === 0 ) ? ['sort ' => $ each [1 ], 'order ' => $ configDefaultOrder ] : [
183
156
'sort ' => $ each [0 ],
184
157
'order ' => $ each [1 ],
185
158
];
186
159
}
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