@@ -20,6 +20,13 @@ abstract class AbstractDatatable implements DatatableDriverInterface
2020 */
2121 protected $ whereColumns = [];
2222
23+ /**
24+ * Columns for having conditions
25+ *
26+ * @var array
27+ */
28+ protected $ havingColumns = [];
29+
2330 /**
2431 * Searchable Columns for where conditions
2532 *
@@ -188,21 +195,30 @@ protected function setColumns()
188195 protected function setWhereColumns ()
189196 {
190197 foreach ($ this ->query ->columns as $ c ) {
191- if (!strpos ($ c , '_id ' )) {
198+ if (gettype ($ c ) === 'object ' ){
199+ $ c = $ c ->getValue ();
200+ if (strpos ($ c , ' as ' )) {
201+ $ column = explode (' as ' , $ c );
202+ if (in_array (trim ($ column [1 ]), $ this ->searchColumns , true )) {
203+ $ this ->havingColumns [] = trim ($ column [1 ]);
204+ }
205+ }
206+ }
207+ else if (!strpos ($ c , '_id ' )) {
192208 if (strpos ($ c , ' as ' )) {
193209 $ column = explode (' as ' , $ c );
194210 if (in_array (trim ($ column [1 ]), $ this ->searchColumns , true )) {
195- $ this ->whereColumns [] = $ column [0 ];
211+ $ this ->whereColumns [] = trim ( $ column [0 ]) ;
196212 }
197213
198214 } else {
199215 if (isset (explode ('. ' , $ c )[1 ])) {
200216 if (in_array (explode ('. ' , $ c )[1 ], $ this ->searchColumns , true )) {
201- $ this ->whereColumns [] = $ c ;
217+ $ this ->whereColumns [] = trim ( $ c ) ;
202218 }
203219
204220 } else {
205- $ this ->whereColumns [] = $ c ;
221+ $ this ->whereColumns [] = trim ( $ c ) ;
206222 }
207223 }
208224 }
@@ -251,9 +267,9 @@ protected function checkIfQueryIsForSearchingPurpose()
251267 */
252268 protected function setTotalDataAndFiltered ()
253269 {
254- if ( ! $ this ->totalData )
255- {
256- // to get correct result count in case of group by
270+ if ( ! $ this ->totalData )
271+ {
272+ // to get correct result count in case of group by
257273 if ( $ this ->query ->groups )
258274 {
259275 $ this ->totalData = $ this ->query ->getCountForPagination ();
@@ -264,11 +280,22 @@ protected function setTotalDataAndFiltered()
264280 }
265281
266282 $ this ->totalFiltered = $ this ->totalData ;
267- }
268- else
269- {
270- $ this ->totalFiltered = $ this ->query ->count ();
271- }
283+ }
284+ else
285+ {
286+
287+ $ this ->totalFiltered = $ this ->query ->count ();
288+ if ( !$ this ->totalFiltered )
289+ {
290+ if (!empty ($ this ->havingColumns ))
291+ {
292+ $ this ->query ->bindings ['where ' ] = [];
293+ $ this ->query ->wheres = [];
294+ $ this ->havingCondition ($ this ->request ->getSearchString (), $ this ->havingColumns );
295+ $ this ->totalFiltered = $ this ->query ->count ();
296+ }
297+ }
298+ }
272299 }
273300
274301 /**
@@ -318,7 +345,6 @@ protected function searchQuery()
318345 if (!empty ($ this ->whereColumns )) {
319346 $ this ->query = $ this ->condition ($ this ->request ->getSearchString (), $ this ->whereColumns );
320347 }
321-
322348 }
323349
324350 /**
@@ -328,27 +354,55 @@ protected function searchQuery()
328354 *
329355 * @return mixed
330356 */
331- protected function condition ($ search , $ columns )
357+ protected function condition ($ search , $ columns, $ type = ' Where ' )
332358 {
333359 return $ this ->query ->where (function ($ q ) use ($ search , $ columns ) {
334360 $ q ->where ($ columns [0 ], 'LIKE ' , "% {$ search }% " );
335- return $ this ->nestedWheres ($ q );
361+ return $ this ->nestedWheres ($ q, $ search );
336362 });
337363 }
338364
365+ /**
366+ * Apply having clause on query
367+ * @param string $search
368+ * @param array $columns
369+ *
370+ * @return mixed
371+ */
372+ protected function havingCondition ($ search , $ columns )
373+ {
374+ $ this ->query ->havingRaw ("{$ columns [0 ]} LIKE '% {$ search }%' " );
375+ return $ this ->nestedHaving ($ search );
376+ }
377+
339378 /**
340379 * Return all where conditions to be nested
341380 *
342381 * @param mixed $q
382+ * @param string $search search string
343383 *
344384 * @return \Illuminate\Database\Eloquent\Builder instance
345385 */
346- protected function nestedWheres ($ q )
386+ protected function nestedWheres ($ q, $ search )
347387 {
348388 for ($ i = 1 ; $ i < count ($ this ->whereColumns ); $ i ++) {
349- $ q ->orWhere ($ this ->whereColumns [$ i ], 'LIKE ' , "% {$ this ->request ->getSearchString ()}% " );
389+ $ q ->orWhere ($ this ->whereColumns [$ i ], 'LIKE ' , "% {$ search }% " );
390+ }
391+ return $ q ;
392+ }
393+
394+ /**
395+ * Return all having clauses to be nested
396+ *
397+ * @param string $type search string
398+ *
399+ * @return \Illuminate\Database\Eloquent\Builder instance
400+ */
401+ protected function nestedHaving ($ search )
402+ {
403+ for ($ i = 1 ; $ i < count ($ this ->havingColumns ); $ i ++) {
404+ $ this ->query ->orHavingRaw ("{$ this ->havingColumns [$ i ]} LIKE '% {$ search }%' " );
350405 }
351- return $ q ;
352406 }
353407
354408 /**
@@ -385,7 +439,6 @@ public function response()
385439 public function jsonResponse ()
386440 {
387441 return json_encode ($ this ->response ());
388-
389442 }
390443
391444 /**
@@ -453,7 +506,6 @@ public function addColumns(array $column)
453506 foreach ($ this ->result as $ r ) {
454507 $ r ->$ c = $ cols ->call ($ this , $ r );
455508 }
456-
457509 }
458510 return $ this ;
459511 }
@@ -488,3 +540,4 @@ public function getQuery()
488540 return $ this ->query ;
489541 }
490542}
543+
0 commit comments