Skip to content

Commit

Permalink
Merge pull request #18 from iYogesharma/3.x-patch-1
Browse files Browse the repository at this point in the history
3.x patch 1
  • Loading branch information
iYogesharma authored Jun 10, 2022
2 parents f2a5e88 + 5071d72 commit a343a0f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 21 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"datatable",
"laravel"
],
"version": "3.3.2",
"version": "3.4",
"license": "MIT",
"authors": [{
"name": "Yogesh Sharma",
Expand Down
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 a343a0f

Please sign in to comment.