File tree Expand file tree Collapse file tree 4 files changed +36
-16
lines changed Expand file tree Collapse file tree 4 files changed +36
-16
lines changed Original file line number Diff line number Diff line change 66 " datatable" ,
77 " laravel"
88 ],
9- "version" : " 4.0 " ,
9+ "version" : " 4.1 " ,
1010 "license" : " MIT" ,
1111 "authors" : [{
1212 "name" : " Yogesh Sharma" ,
Original file line number Diff line number Diff line change @@ -266,16 +266,14 @@ protected function setArrayFilters( array $filters )
266266 }
267267
268268
269- /**
269+ /**
270270 * Prepare result to return as response
271271 *
272272 * @return void
273273 */
274274 protected function prepareQueryWithoutOffset ()
275275 {
276- $ this ->query = $ this ->query ->orderBy ($ this ->columns [$ this ->order ],$ this ->dir );
277-
278- $ this ->result = $ this ->query ->get ();
276+ $ this ->result = $ this ->queryWithOrderBy ()->get ();
279277 }
280278
281279 /**
@@ -285,12 +283,26 @@ protected function prepareQueryWithoutOffset()
285283 */
286284 public function prepareQueryWithOffsetAndOrderBy ()
287285 {
288- $ this ->query = $ this ->query ->offset ($ this ->request ->getStart ())
289- ->limit ($ this ->request ->getPerPage ())
290- ->orderBy ($ this ->columns [$ this ->order ],$ this ->dir );
286+ $ this ->query = $ this ->queryWithOrderBy ()
287+ ->offset ($ this ->request ->getStart ())
288+ ->limit ($ this ->request ->getPerPage ());
289+
291290 $ this ->result = $ this ->query ->get ();
292291 }
293292
293+ /**
294+ * Add Order By Clause to Query IF Column list is not empty
295+ */
296+ private function queryWithOrderBy ()
297+ {
298+ if ( isset ( $ this ->columns [$ this ->order ] ) )
299+ {
300+ $ this ->query = $ this ->query ->orderBy ($ this ->columns [$ this ->order ],$ this ->dir );
301+ }
302+
303+ return $ this ->query ;
304+ }
305+
294306 /**
295307 * Prepare result to return as response
296308 *
Original file line number Diff line number Diff line change 44
55 class DatatableRequest
66 {
7+ const LIMIT = 100 ;
8+
9+ const OFFSET = 0 ;
10+
711 /**
812 * @var \Illuminate\Http\Request
913 */
@@ -73,20 +77,20 @@ public function getDraw()
7377 }
7478
7579 /**
76- * @return string
80+ * @return int
7781 */
7882 public function getStart ()
7983 {
80- return $ this ->request ->input ('start ' );
84+ return $ this ->request ->input ('start ' ) ?? self :: OFFSET ;
8185 }
8286
8387 /**
8488 * Get max data per page for pagination
85- * @return array|string
89+ * @return array|int
8690 */
8791 public function getPerPage ()
8892 {
89- return $ this ->request ->input ('length ' );
93+ return $ this ->request ->input ('length ' ) ?? self :: LIMIT ;
9094 }
9195
9296 /**
Original file line number Diff line number Diff line change @@ -53,11 +53,15 @@ private function identifyQueryColumns()
5353 {
5454 $ skip = config ('datatable.skip ' ) ?? [];
5555
56+ $ connection = $ this ->query ->connection ->getDatabaseName ();
57+
5658 if ( empty ($ this ->query ->columns ) || $ this ->query ->columns [0 ] === '* ' )
5759 {
58- $ this ->query ->columns = Schema::getColumnListing ( $ this ->query ->from );
60+ $ from = strpos ($ this ->query ->from ,'. ' ) ? explode ('. ' ,$ this ->query ->from )[1 ] : $ this ->query ->from ;
61+
62+ $ this ->query ->columns = Schema::connection ($ connection )->getColumnListing ( $ from );
5963
60- delete_keys ($ this ->query ->columns , $ skip );
64+ ! empty ( $ skip ) && delete_keys ($ this ->query ->columns , $ skip );
6165 }
6266 else
6367 {
@@ -69,9 +73,9 @@ private function identifyQueryColumns()
6973
7074 $ table = explode ('.* ' ,$ c )[0 ];
7175
72- $ columns = Schema::getColumnListing ( $ table );
76+ $ columns = Schema::connection ( $ connection )-> getColumnListing ( $ table );
7377
74- delete_keys ($ columns , $ skip );
78+ ! empty ( $ skip ) && delete_keys ($ columns , $ skip );
7579
7680 array_walk ($ columns , function (&$ value )use ($ table ) { $ value = "{$ table }. {$ value }" ; } );
7781
You can’t perform that action at this time.
0 commit comments