Skip to content

Commit

Permalink
Support For Having Clause In Query
Browse files Browse the repository at this point in the history
Added support for having clause in case of raw queries in select statement
System is throwing error in search query  if query builder contains Raw columns (columns  that are instance of Illuminate\Database\Query\Expression class)
This commit will fix this issue in search query by introducing having clause in case of  raw/expression columns
  • Loading branch information
iYogesharma authored Jun 10, 2022
1 parent f2a5e88 commit 3d20f2a
Showing 1 changed file with 73 additions and 20 deletions.
93 changes: 73 additions & 20 deletions src/AbstractDatatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ abstract class AbstractDatatable implements DatatableDriverInterface
*/
protected $whereColumns = [];

/**
* Columns for having conditions
*
* @var array
*/
protected $havingColumns = [];

/**
* Searchable Columns for where conditions
*
Expand Down Expand Up @@ -188,21 +195,30 @@ protected function setColumns()
protected function setWhereColumns()
{
foreach ($this->query->columns as $c) {
if (!strpos($c, '_id')) {
if(gettype($c) === 'object'){
$c = $c->getValue();
if (strpos($c, ' as ')) {
$column = explode(' as ', $c);
if (in_array(trim($column[1]), $this->searchColumns, true)) {
$this->havingColumns[] = trim($column[1]);
}
}
}
else if (!strpos($c, '_id')) {
if (strpos($c, ' as ')) {
$column = explode(' as ', $c);
if (in_array(trim($column[1]), $this->searchColumns, true)) {
$this->whereColumns[] = $column[0];
$this->whereColumns[] = trim($column[0]);
}

} else {
if (isset(explode('.', $c)[1])) {
if (in_array(explode('.', $c)[1], $this->searchColumns, true)) {
$this->whereColumns[] = $c;
$this->whereColumns[] = trim($c);
}

} else {
$this->whereColumns[] = $c;
$this->whereColumns[] = trim($c);
}
}
}
Expand Down Expand Up @@ -251,9 +267,9 @@ protected function checkIfQueryIsForSearchingPurpose()
*/
protected function setTotalDataAndFiltered()
{
if( ! $this->totalData )
{
// to get correct result count in case of group by
if( ! $this->totalData )
{
// to get correct result count in case of group by
if( $this->query->groups )
{
$this->totalData = $this->query->getCountForPagination();
Expand All @@ -264,11 +280,22 @@ protected function setTotalDataAndFiltered()
}

$this->totalFiltered = $this->totalData;
}
else
{
$this->totalFiltered = $this->query->count();
}
}
else
{

$this->totalFiltered = $this->query->count();
if( !$this->totalFiltered )
{
if (!empty($this->havingColumns))
{
$this->query->bindings['where'] = [];
$this->query->wheres = [];
$this->havingCondition($this->request->getSearchString(), $this->havingColumns);
$this->totalFiltered = $this->query->count();
}
}
}
}

/**
Expand Down Expand Up @@ -318,7 +345,6 @@ protected function searchQuery()
if (!empty($this->whereColumns)) {
$this->query = $this->condition($this->request->getSearchString(), $this->whereColumns);
}

}

/**
Expand All @@ -328,27 +354,55 @@ protected function searchQuery()
*
* @return mixed
*/
protected function condition($search, $columns)
protected function condition($search, $columns, $type = 'Where')
{
return $this->query->where(function ($q) use ($search, $columns) {
$q->where($columns[0], 'LIKE', "%{$search}%");
return $this->nestedWheres($q);
return $this->nestedWheres($q,$search);
});
}

/**
* Apply having clause on query
* @param string $search
* @param array $columns
*
* @return mixed
*/
protected function havingCondition($search, $columns )
{
$this->query->havingRaw("{$columns[0]} LIKE '%{$search}%'");
return $this->nestedHaving($search);
}

/**
* Return all where conditions to be nested
*
* @param mixed $q
* @param string $search search string
*
* @return \Illuminate\Database\Eloquent\Builder instance
*/
protected function nestedWheres($q)
protected function nestedWheres($q,$search)
{
for ($i = 1; $i < count($this->whereColumns); $i++) {
$q->orWhere($this->whereColumns[$i], 'LIKE', "%{$this->request->getSearchString()}%");
$q->orWhere($this->whereColumns[$i], 'LIKE', "%{$search}%");
}
return $q;
}

/**
* Return all having clauses to be nested
*
* @param string $type search string
*
* @return \Illuminate\Database\Eloquent\Builder instance
*/
protected function nestedHaving($search)
{
for ($i = 1; $i < count($this->havingColumns); $i++) {
$this->query->orHavingRaw("{$this->havingColumns[$i]} LIKE '%{$search}%'");
}
return $q;
}

/**
Expand Down Expand Up @@ -385,7 +439,6 @@ public function response()
public function jsonResponse()
{
return json_encode($this->response());

}

/**
Expand Down Expand Up @@ -453,7 +506,6 @@ public function addColumns(array $column)
foreach ($this->result as $r) {
$r->$c = $cols->call($this, $r);
}

}
return $this;
}
Expand Down Expand Up @@ -488,3 +540,4 @@ public function getQuery()
return $this->query;
}
}

0 comments on commit 3d20f2a

Please sign in to comment.